Excel用户窗体多个组合框

时间:2018-07-22 16:25:00

标签: excel vba excel-vba

我有一个带有(2)ComboBoxes的Excel UserForm。两个组合框都列出了“ H”范围内的信息,而ComboBox2应该列出了“ V”范围内的信息。有什么想法我在这里做错了吗?我对VBA还是很陌生,我知道我的代码可能很草率。请保持通俗易懂,谢谢!

Private Sub Userform_Initialize()
LookAhead.Caption = Span & " Week Look Ahead"

' Sets range for ComboBox lists
    Dim rng As Range, r As Range, rng2 As Range, r2 As Range

    Set rng = Sheet1.Range("H2:H65536")
        For Each r In rng
            AddUnique r.value
        Next r
    Set rng2 = Sheet1.Range("V2:V65536")
        For Each r2 In rng2
            AddUnique r2.value
        Next r2

    End Sub

' Filter out duplicates in lists for UserForm ComboBoxes

Sub AddUnique(value As Variant)
    Dim i As Integer
    Dim inList As Boolean

    inList = False
    With Me.ComboBox1
        For i = 0 To Me.ComboBox1.ListCount - 1
            If Me.ComboBox1.List(i) = value Then
                inList = True
                Exit For
            End If
        Next i

        If Not inList Then
            .AddItem value
        End If
    End With

    Dim ii As Integer
    Dim inList2 As Boolean

    inList2 = False
    With Me.ComboBox2
        For ii = 0 To Me.ComboBox2.ListCount - 1
            If Me.ComboBox2.List(ii) = value Then
                inList2 = True
                Exit For
            End If
        Next ii

        If Not inList2 Then
            .AddItem value
        End If
    End With
End Sub

2 个答案:

答案 0 :(得分:1)

成功!我做到了,它似乎正在起作用:

Dim Cl As Range
Dim Area As String
Dim TCR As String

    With CreateObject("scripting.dictionary")
        For Each Cl In Sheets("Projects").ListObjects("Table1").ListColumns(22).DataBodyRange
            If Not .exists(Cl.value) Then .Add Cl.value, Nothing
        Next Cl
         ComboBox1.Clear
         ComboBox1.List = Application.Transpose(.keys)
         .RemoveAll
    End With

    With CreateObject("scripting.dictionary")
        For Each Cl In Sheets("Projects").ListObjects("Table1").ListColumns(8).DataBodyRange
            If Not .exists(Cl.value) Then .Add Cl.value, Nothing
        Next Cl
         ComboBox2.Clear
         ComboBox2.List = Application.Transpose(.keys)
         .RemoveAll
    End With
    End Sub

答案 1 :(得分:0)

AddUnique子目录中,您正在传递要添加的值作为参数,然后将该值同时添加到ComboBox1ComboBox2中。由于您想向每个组合框添加不同的值,因此需要对代码进行些微调整,以便您也可以通过某种方式来确定要将值添加到哪个组合框。

一种方式在下面包含的代码中。我没有直接引用每个组合框,而是添加了一个组合框参数。然后,在调用AddUnique时,您还可以指定应将值添加到哪个组合框。

另一种选择(假设您在此处仅使用两个组合框)将具有一个布尔参数,该参数为true时会将值添加到Combobox1,如果为false则将添加值到Combobox2

或者,而不是实际传入组合框,而是将参数更改为控件名称的字符串,然后通过Me.Controls(control_name)

访问控件
Private Sub Userform_Initialize()
LookAhead.Caption = Span & " Week Look Ahead"

' Sets range for ComboBox lists
    Dim rng As Range, r As Range, rng2 As Range, r2 As Range

    Set rng = Sheet1.Range("H2:H65536")
        For Each r In rng
            AddUnique r.value, Me.ComboBox1
        Next r
    Set rng2 = Sheet1.Range("V2:V65536")
        For Each r2 In rng2
            AddUnique r2.value, Me.ComboBox2
        Next r2

    End Sub

' Filter out duplicates in lists for UserForm ComboBoxes

Sub AddUnique(value As Variant, combobox as Variant)
    Dim i As Integer
    Dim inList As Boolean

    inList = False
    With combobox
        For i = 0 To .ListCount - 1
            If .List(i) = value Then
                inList = True
                Exit For
            End If
        Next i

        If Not inList Then
            .AddItem value
        End If
    End With
End Sub