Excel data validation with suggestions/autocomplete

后端 未结 7 2221
一整个雨季
一整个雨季 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 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?

提交回复
热议问题