Excel data validation with suggestions/autocomplete

后端 未结 7 2179
一整个雨季
一整个雨季 2020-12-03 07:42

Apologies for my low level of Excel understanding, maybe what I am looking to do is not possible.

I have a list of 120 entries that I want to use as data validatio

相关标签:
7条回答
  • 2020-12-03 07:55

    If you don't want to go down the VBA path, there is this trick from a previous question.

    Excel 2010: how to use autocomplete in validation list

    It does add some annoying bulk to the top of your sheets, and potential maintenance (should you need more options, adding names of people from a staff list, new projects etc.) but works all the same.

    0 讨论(0)
  • 2020-12-03 07:59

    None of the above mentioned solution worked. The one that seemed to work only provide the functionality for just one cell

    Recently I had to enter a lot of names and without suggestions, it was a huge pain. I was fortunate enough to have this excel autocomplete add-in to enable the autocompletion. The down side is that you need to enable macro (but you can always turn it off later)

    0 讨论(0)
  • 2020-12-03 08:00

    I adapted the answer by ChrisB. Like in his example a temporary combobox is made visible when a cell is clicked. Additionally:

    1. List of Combobox items is updated as user types, only matching items are displayed
    2. if any item from combobox is selected, filtering is skipped as it makes sense and because of this error

    Option Explicit
    
    Private Const DATA_RANGE = "A1:A16"
    Private Const DROPDOWN_RANGE = "F2:F10"
    Private Const HELP_COLUMN = "$G"
    
    
    Private Sub Worksheet_SelectionChange(ByVal target As Range)
        Dim xWs As Worksheet
        Set xWs = Application.ActiveSheet
        
        On Error Resume Next
        
        With Me.TempCombo
            .LinkedCell = vbNullString
            .Visible = False
        End With
        
        If target.Cells.count > 1 Then
            Exit Sub
        End If
        
        Dim isect As Range
        Set isect = Application.Intersect(target, Range(DROPDOWN_RANGE))
        If isect Is Nothing Then
           Exit Sub
        End If
           
        With Me.TempCombo
            .Visible = True
            .Left = target.Left - 1
            .Top = target.Top - 1
            .Width = target.Width + 5
            .Height = target.Height + 5
            .LinkedCell = target.Address
    
        End With
    
        Me.TempCombo.Activate
        Me.TempCombo.DropDown
    End Sub
    
    Private Sub TempCombo_Change()
        If Me.TempCombo.Visible = False Then
            Exit Sub
        End If
        
        Dim currentValue As String
        currentValue = Range(Me.TempCombo.LinkedCell).Value
        
        If Trim(currentValue & vbNullString) = vbNullString Then
            Me.TempCombo.ListFillRange = "=" & DATA_RANGE
        Else
            If Me.TempCombo.ListIndex = -1 Then
                 Dim listCount As Integer
                 listCount = write_matching_items(currentValue)
                 Me.TempCombo.ListFillRange = "=" & HELP_COLUMN & "1:" & HELP_COLUMN & listCount
                 Me.TempCombo.DropDown
            End If
    
        End If
    End Sub
    
    
    Private Function write_matching_items(currentValue As String) As Integer
        Dim xWs As Worksheet
        Set xWs = Application.ActiveSheet
    
        Dim cell As Range
        Dim c As Range
        Dim firstAddress As Variant
        Dim count As Integer
        count = 0
        xWs.Range(HELP_COLUMN & ":" & HELP_COLUMN).Delete
        With xWs.Range(DATA_RANGE)
            Set c = .Find(currentValue, LookIn:=xlValues)
            If Not c Is Nothing Then
                firstAddress = c.Address
                Do
                  Set cell = xWs.Range(HELP_COLUMN & "$" & (count + 1))
                  cell.Value = c.Value
                  count = count + 1
                 
                  Set c = .FindNext(c)
                  If c Is Nothing Then
                    GoTo DoneFinding
                  End If
               Loop While c.Address <> firstAddress
            End If
    DoneFinding:
        End With
        
        write_matching_items = count
    
    End Function
    
    Private Sub TempCombo_KeyDown( _
                    ByVal KeyCode As MSForms.ReturnInteger, _
                    ByVal Shift As Integer)
    
        Select Case KeyCode
            Case 9  ' Tab key
                Application.ActiveCell.Offset(0, 1).Activate
            Case 13 ' Pause key
                Application.ActiveCell.Offset(1, 0).Activate
        End Select
    End Sub

    Notes:

    1. ComboBoxe's MatchEntry must be set to 2 - fmMatchEntryNone. Don't forget to set ComboBox name to TempCombo
    2. I am using listFillRange to set ComboBox options. The range must be continuous, so, matching items are stored in a help column.
    3. I have tried accomplishing the same with ComboBox.addItem, but it turned out to be hard to repaint list box as user types
    0 讨论(0)
  • 2020-12-03 08:04

    Why not use a Pivot?

    Sample Table Filter Pivot

    0 讨论(0)
  • 2020-12-03 08:05

    ExtendOffice.com offers a VBA solution that worked for me in Excel 2016. Here's my description of the steps. I included additional details to make it easier. I also modified the VBA code slightly. If this doesn't work for you, retry the steps or check out the instructions on the ExtendOffice page.

    1. Add data validation to a cell (or range of cells). Allow = List. Source = [the range with the values you want for the auto-complete / drop-down]. Click OK. You should now have a drop-down but with a weak auto-complete feature.

    2. With a cell containing your newly added data validation, insert an ActiveX combo box (NOT a form control combo box). This is done from the Developer ribbon. If you don't have the Developer ribbon you will need to add it from the Excel options menu.

    3. From the Developer tab in the Controls section, click "Design Mode". Select the combo box you just inserted. Then in the same ribbon section click "Properties". In the Properties window, change the name of the combo box to "TempComboBox".

    4. Press ALT + F11 to go to the Visual Basic Editor. On the left-hand side, double click the worksheet with your data validation to open the code for that sheet. Copy and paste the following code onto the sheet. NOTE: I modified the code slightly so that it works even with Option Explicit enabled at the top of the sheet.

      Option Explicit
      
      Private Sub Worksheet_SelectionChange(ByVal target As Range)
      'Update by Extendoffice: 2018/9/21
      ' Update by Chris Brackett 2018-11-30
      
      Dim xWs As Worksheet
      Set xWs = Application.ActiveSheet
      
      On Error Resume Next
      
      Dim xCombox As OLEObject
      Set xCombox = xWs.OLEObjects("TempCombo")
      
      ' Added this to auto select all text when activating the combox box.
      xCombox.SetFocus
      
      With xCombox
          .ListFillRange = vbNullString
          .LinkedCell = vbNullString
          .Visible = False
      End With
      
      
      Dim xStr As String
      Dim xArr
      
      
      If target.Validation.Type = xlValidateList Then
          ' The target cell contains Data Validation.
      
          target.Validation.InCellDropdown = False
      
      
          ' Cancel the "SelectionChange" event.
          Dim Cancel As Boolean
          Cancel = True
      
      
          xStr = target.Validation.Formula1
          xStr = Right(xStr, Len(xStr) - 1)
      
          If xStr = vbNullString Then Exit Sub
      
          With xCombox
              .Visible = True
              .Left = target.Left
              .Top = target.Top
              .Width = target.Width + 5
              .Height = target.Height + 5
              .ListFillRange = xStr
      
              If .ListFillRange = vbNullString Then
                  xArr = Split(xStr, ",")
                  Me.TempCombo.List = xArr
              End If
      
              .LinkedCell = target.Address
      
          End With
      
          xCombox.Activate
          Me.TempCombo.DropDown
      
      End If
      End Sub
      
      Private Sub TempCombo_KeyDown( _
                      ByVal KeyCode As MSForms.ReturnInteger, _
                      ByVal Shift As Integer)
          Select Case KeyCode
              Case 9  ' Tab key
                  Application.ActiveCell.Offset(0, 1).Activate
              Case 13 ' Pause key
                  Application.ActiveCell.Offset(1, 0).Activate
          End Select
      End Sub
      
    5. Make sure the the "Microsoft Forms 2.0 Object Library" is referenced. In the Visual Basic Editor, go to Tools > References, check the box next to that library (if not already checked) and click OK. To verify that it worked, go to Debug > Compile VBA Project.

    6. Finally, save your project and click in a cell with the data validation you added. You should see a combo box with a drop-down list of suggestions that updates with each letter you type.

    0 讨论(0)
  • 2020-12-03 08:10

    This is a solution how to make autocomplete drop down list with VBA :


    Firstly you need to insert a combo box into the worksheet and change its properties, and then running the VBA code to enable the autocomplete.

    1. Get into the worksheet which contains the drop down list you want it to be autocompleted.

    2. Before inserting the Combo box, you need to enable the Developer tab in the ribbon.

    a). In Excel 2010 and 2013, click File > Options. And in the Options dialog box, click Customize Ribbon in the right pane, check the Developer box, then click the OK button.

    b). In Outlook 2007, click Office button > Excel Options. In the Excel Options dialog box, click Popular in the right bar, then check the Show Developer tabin the Ribbon box, and finally click the OK button.

    1. Then click Developer > Insert > Combo Box under ActiveX Controls.

    2. Draw the combo box in current opened worksheet and right click it. Select Properties in the right-clicking menu.

    3. Turn off the Design Mode with clicking Developer > Design Mode.

    4. Right click on the current opened worksheet tab and click View Code.

    5. Make sure that the current worksheet code editor is opened, and then copy and paste the below VBA code into it.

    Code borrowed from extendoffice.com

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Update by Extendoffice: 2018/9/21
        Dim xCombox As OLEObject
        Dim xStr As String
        Dim xWs As Worksheet
        Dim xArr
    
    
    
        Set xWs = Application.ActiveSheet
        On Error Resume Next
        Set xCombox = xWs.OLEObjects("TempCombo")
        With xCombox
            .ListFillRange = ""
            .LinkedCell = ""
            .Visible = False
        End With
        If Target.Validation.Type = 3 Then
            Target.Validation.InCellDropdown = False
            Cancel = True
            xStr = Target.Validation.Formula1
            xStr = Right(xStr, Len(xStr) - 1)
            If xStr = "" Then Exit Sub
            With xCombox
                .Visible = True
                .Left = Target.Left
                .Top = Target.Top
                .Width = Target.Width + 5
                .Height = Target.Height + 5
                .ListFillRange = xStr
                If .ListFillRange = "" Then
                    xArr = Split(xStr, ",")
                    Me.TempCombo.List = xArr
                End If
                .LinkedCell = Target.Address
            End With
            xCombox.Activate
            Me.TempCombo.DropDown
        End If
    End Sub
    
    Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        Select Case KeyCode
            Case 9
                Application.ActiveCell.Offset(0, 1).Activate
            Case 13
                Application.ActiveCell.Offset(1, 0).Activate
        End Select
    End Sub
    
    1. Click File > Close and Return to Microsoft Excel to close the Microsoft Visual Basic for Application window.

    2. Now, just click the cell with drop down list, you can see the drop-down list is displayed as a combo box, then type the first letter into the box, the corresponding word will be completed automatically.

    Note: This VBA code is not applied to merged cells.

    Source : How To Autocomplete When Typing In Excel Drop Down List?

    0 讨论(0)
提交回复
热议问题