如何防止Excel组合框显示重复的条目和/或空格?

时间:2018-04-28 16:29:34

标签: excel vba

我创建了一个Excel工作表(名为“users”),其中包含A列中的随机名称列表。此列中的每个单元格都链接到数据验证单元格。当用户在此数据验证单元格中键入字符时选择下拉箭头,此字符(或单词)作为参数传递给每个名称的Excel搜索功能。如果搜索结果返回1,则该名称将作为动态列表的一部分合并(D列)反过来,它显示在数据验证单元格中。这可以在下面看到,我输入了字符'A'并返回了一个以这个字符开头的名字列表。

enter image description here

我通过创建一个单独的工作表(名为“master”)来进一步发展,其中A列现在在每个单元格中包含一个数据验证列表。我已经使用了VBA代码,以便当用户双击其中一个数据验证单元格时,其引用作为参数传递给先前在“用户”表单中使用的搜索功能。请参阅以下内容:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Dim str As String
    Dim cboTemp As OLEObject
    Dim SortCell As Variant

    Dim master As Worksheet
    Dim users As Worksheet


    Set master = ThisWorkbook.Sheets("master")
    Set users = ThisWorkbook.Sheets("users")
    Set cboTemp = master.OLEObjects("DataCombo")

    On Error Resume Next

On Error GoTo errHandler

   'If found Data Validation cell
    If Target.Validation.Type = 3 Then
       'MsgBox Target.Address
        SortCell = "master!" & Target.Address

        'Set Target Address to that of search function in InCell column
        With users
            .Range("B2").Value = "=IF(IFERROR(SEARCH(" & SortCell & ",A2,1), 0)=1,1,0)"
            .Range("B2:B131").FillDown
        End With 
    End If

我再次取得了进展,在这种情况下,当用户双击工作表“master”的A列中的其中一个数据验证列表时,会出现一个组合框,其中包含来自所有随机名称的下拉列表“用户”表。当用户键入组合框时,将过滤名称列表以反映在“用户”列D中创建的动态列表。完整的VBA代码如下所示:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Dim str As String
    Dim cboTemp As OLEObject
    Dim SortCell As Variant

    Dim master As Worksheet
    Dim users As Worksheet


    Set master = ThisWorkbook.Sheets("master")
    Set users = ThisWorkbook.Sheets("users")
    Set cboTemp = master.OLEObjects("DataCombo")

    On Error Resume Next

On Error GoTo errHandler

    'If found Data Validation cell
    If Target.Validation.Type = 3 Then

        'if the cell contains a data validation list
        Cancel = True
        Application.EnableEvents = False

        'MsgBox Target.Address
        SortCell = "master!" & Target.Address

        'Set Target Address to that of search function in InCell column
        With users
            .Range("B2").Value = "=IF(IFERROR(SEARCH(" & SortCell & ",A2,1), 0)=1,1,0)"
            .Range("B2:B131").FillDown
        End With

        'Get Data Validation Formula
        str = Target.Validation.Formula1
        str = Right(str, Len(str) - 1)

        'MsgBox str

        With cboTemp
          'show the combobox with the list
          .Visible = True
          .Left = Target.Left
          .Top = Target.Top
          .Width = Target.Width + 5
          .Height = Target.Height + 5 
          .ListFillRange = str
          .LinkedCell = Target.Address
        End With

        cboTemp.Activate
        Me.DataCombo.DropDown

    End If

errHandler:
  Application.EnableEvents = True
  Exit Sub

End Sub

我遇到的问题是,当用户键入组合框并因此根据键入的字符创建新的名称列表时,组合框保留动态列表的原始长度 - 它不会减小列表的大小以反映从搜索返回的名称数量减少。此外,留下空白并重复名称以“弥补”这个剩余长度。请参阅下面的图片,其中说明了这一点:

enter image description here

我为我的问题的长度和详细信息道歉,但我想知道如何能够阻止组合框以这种方式运行并使其能够动态地减少其下拉列表大小以匹配长度返回的名字列表?

欢迎任何想法/建议。

1 个答案:

答案 0 :(得分:0)

这段代码将调整组合框下拉列表的大小,以匹配在" users"中创建的动态列表的大小。片:

Private Sub DataCombo_Change()

   'When user types into the combo box, this will resize the dropdown list to match the length of the dynamic list created in "users"
    With Me.DataCombo
          .Visible = True
          .ListFillRange = "Employees"
    End With

End Sub

每次用户在组合框中键入内容以搜索名称时,都会执行上述代码。