Excel 2010:如何在验证列表中使用自动完成功能

时间:2011-11-03 02:53:22

标签: excel vba excel-vba excel-2010

我正在使用一个大型验证列表,其中包含几个vlookup()函数。这个清单越来越大了。有没有办法输入我正在寻找的列表项的第一个字母,而不是手动向下滚动列表搜索项目?

我已经完成了一些谷歌搜索,但这表明这在早期版本的Excel中确实是可行的,但在Excel 2010中确实不可能。希望你们能提供帮助。

6 个答案:

答案 0 :(得分:11)

这是处理此问题的一种非常好的方法(在ozgrid上找到):

假设您的列表位于Sheet2,并且您希望在Sheet1上使用带有自动完成功能的验证列表。

Sheet1 A1上输入=Sheet2!A1并根据需要复制包含尽可能多的备用行(总共300行)。隐藏这些行并在“引用:”中使用此公式,以获取名为MyList的动态命名范围:

=OFFSET(Sheet1!$A$1,0,0,MATCH("*",Sheet1!$A$1:$A$300,-1),1)

现在位于最后一个隐藏行的正下方的单元格中使用数据验证,对于列表来源使用=MyList

[编辑] 适用于Excel 2007+的版本(虽然无法在2010年进行测试,但AFAIK没有真正特定的版本)。
假设您的数据源位于Sheet2!A1:A300,我们假设您的验证列表(又名自动填充)位于单元格Sheet1!A1上。

  1. 创建一个动态命名范围MyList,它将取决于您进行验证的单元格的值

    =OFFSET(Sheet2!$A$1,MATCH(Sheet1!$A$1&"*",Sheet2!$A$1:$A$300,0)-1,0,COUNTA(Sheet2!$A:$A))

  2. 在单元格Sheet1!A1上添加验证列表,该列表将重新列入列表=MyList

  3. <强>注意事项

    1. 这不是真正的自动完成,因为您必须先键入然后单击验证箭头:然后列表将在第一个匹配元素处开始你的清单

    2. 该列表将一直持续到您的数据结束。如果您想更精确(仅在列表中保留匹配的元素),您可以使用COUNTA更改SUMLPRODUCT,以计算匹配元素的数量< / p>

    3. 您的来源列表必须排序

答案 1 :(得分:1)

只要您有垂直的项目列,Excel就会自动执行此操作。如果您选择列下方(或上方)的空白单元格并开始输入,则会根据列中的所有内容自动完成。

答案 2 :(得分:1)

JMax的答案的基础上,将此公式用于动态命名范围,以使解决方案适用于多行:

=OFFSET(Sheet2!$A$1,MATCH(INDIRECT("Sheet1!"&ADDRESS(ROW(),COLUMN(),4))&"*",Sheet2!$A$1:$A$300,0)-1,0,COUNTA(Sheet2!$A:$A))

答案 3 :(得分:0)

=OFFSET(NameList!$A$2:$A$200,MATCH(INDIRECT("FillData!"&ADDRESS(ROW(),COLUMN(),4))&"*",NameList!$A$2:$A$200,0)-1,0,COUNTIF($A$2:$A$200,INDIRECT("FillData!"&ADDRESS(ROW(),COLUMN(),4))&"*"),1)
  1. 将工作表名称创建为Namelist。在A列填写数据列表中。

  2. 创建另一个工作表名称FillData,以便根据需要创建数据验证列表。

  3. 键入第一个字母并选择,下拉菜单将根据您的类型显示。

答案 4 :(得分:0)

这是另一种选择。它的工作原理是在启用验证的情况下将ActiveX ComboBox放在单元格顶部,然后在ComboBox中提供自动完成功能。

Option Explicit

' Autocomplete - replacing validation lists with ActiveX ComboBox
'
' Usage:
'   1. Copy this code into a module named m_autocomplete
'   2. Go to Tools / References and make sure "Microsoft Forms 2.0 Object Library" is checked
'   3. Copy and paste the following code to the worksheet where you want autocomplete
'      ------------------------------------------------------------------------------------------------------
'      - autocomplete
'      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'          m_autocomplete.SelectionChangeHandler Target
'      End Sub
'      Private Sub AutoComplete_Combo_KeyDown(ByVal KeyCode As msforms.ReturnInteger, ByVal Shift As Integer)
'          m_autocomplete.KeyDownHandler KeyCode, Shift
'      End Sub
'      Private Sub AutoComplete_Combo_Click()
'          m_autocomplete.AutoComplete_Combo_Click
'      End Sub
'      ------------------------------------------------------------------------------------------------------

' When the combobox is clicked, it should dropdown (expand)
Public Sub AutoComplete_Combo_Click()
    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim cbo As OLEObject: Set cbo = GetComboBoxObject(ws)
    Dim cb As ComboBox: Set cb = cbo.Object
    If cbo.Visible Then cb.DropDown
End Sub

' Make it easier to navigate between cells
Public Sub KeyDownHandler(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Const UP As Integer = -1
    Const DOWN As Integer = 1

    Const K_TAB_______ As Integer = 9
    Const K_ENTER_____ As Integer = 13
    Const K_ARROW_UP__ As Integer = 38
    Const K_ARROW_DOWN As Integer = 40

    Dim direction As Integer: direction = 0

    If Shift = 0 And KeyCode = K_TAB_______ Then direction = DOWN
    If Shift = 0 And KeyCode = K_ENTER_____ Then direction = DOWN
    If Shift = 1 And KeyCode = K_TAB_______ Then direction = UP
    If Shift = 1 And KeyCode = K_ENTER_____ Then direction = UP
    If Shift = 1 And KeyCode = K_ARROW_UP__ Then direction = UP
    If Shift = 1 And KeyCode = K_ARROW_DOWN Then direction = DOWN

    If direction <> 0 Then ActiveCell.Offset(direction, 0).Activate

    AutoComplete_Combo_Click
End Sub

Public Sub SelectionChangeHandler(ByVal Target As Range)
    On Error GoTo errHandler

    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim cbo As OLEObject: Set cbo = GetComboBoxObject(ws)
    Dim cb As ComboBox: Set cb = cbo.Object

    ' Try to hide the ComboBox. This might be buggy...
    If cbo.Visible Then
        cbo.Left = 10
        cbo.Top = 10
        cbo.ListFillRange = ""
        cbo.LinkedCell = ""
        cbo.Visible = False
        Application.ScreenUpdating = True
        ActiveSheet.Calculate
        ActiveWindow.SmallScroll
        Application.WindowState = Application.WindowState
        DoEvents
    End If

    If Not HasValidationList(Target) Then GoTo ex

    Application.EnableEvents = False

    ' TODO: the code below is a little fragile
    Dim lfr As String
    lfr = Mid(Target.Validation.Formula1, 2)
    lfr = Replace(lfr, "INDIREKTE", "") ' norwegian
    lfr = Replace(lfr, "INDIRECT", "") ' english
    lfr = Replace(lfr, """", "")
    lfr = Application.Range(lfr).Address(External:=True)

    cbo.ListFillRange = lfr
    cbo.Visible = True
    cbo.Left = Target.Left
    cbo.Top = Target.Top
    cbo.Height = Target.Height + 5
    cbo.Width = Target.Width + 15
    cbo.LinkedCell = Target.Address(External:=True)
    cbo.Activate
    cb.SelStart = 0
    cb.SelLength = cb.TextLength
    cb.DropDown

    GoTo ex

errHandler:
    Debug.Print "Error"
    Debug.Print Err.Number
    Debug.Print Err.Description
ex:
    Application.EnableEvents = True
End Sub

' Does the cell have a validation list?
Function HasValidationList(Cell As Range) As Boolean
    HasValidationList = False
    On Error GoTo ex
    If Cell.Validation.Type = xlValidateList Then HasValidationList = True
ex:
End Function

' Retrieve or create the ComboBox
Function GetComboBoxObject(ws As Worksheet) As OLEObject
    Dim cbo As OLEObject
    On Error Resume Next
    Set cbo = ws.OLEObjects("AutoComplete_Combo")
    On Error GoTo 0
    If cbo Is Nothing Then
        'Dim EnableSelection As Integer: EnableSelection = ws.EnableSelection
        Dim ProtectContents As Boolean: ProtectContents = ws.ProtectContents

        Debug.Print "Lager AutoComplete_Combo"
        If ProtectContents Then ws.Unprotect
        Set cbo = ws.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, DisplayAsIcon:=False, _
                            Left:=50, Top:=18.75, Width:=129, Height:=18.75)
        cbo.name = "AutoComplete_Combo"
        cbo.Object.MatchRequired = True
        cbo.Object.ListRows = 12
        If ProtectContents Then ws.Protect
    End If
    Set GetComboBoxObject = cbo
End Function

答案 5 :(得分:0)

正如其他人所说,你需要使用一个组合框。但是,大多数教程都会向您展示如何只设置一个组合框,这个过程非常繁琐。

当我从列表中输入大量数据之前遇到此问题时,我建议您使用this autocomplete add-in。它可以帮助您在所选的任何单元格上创建组合框,并且您可以定义一个列表以显示在下拉列表中。