【发布时间】:2018-07-22 16:25:00
【问题描述】:
我有一个带有 (2) 个组合框的 Excel 用户窗体。两个组合框都列出了“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
【问题讨论】:
-
欢迎来到 Stack Overflow:您不是在问 good, clear, concise question 请阅读 How to ask a good question,然后编辑您的问题以包含代码、预期行为和 出了什么问题...那么我们可以尝试提供帮助
-
上面列出了错误:“两个组合框都列出了“H”范围内的信息,而 ComboBox2 应该列出了“V”范围内的信息。”我似乎无法为 combobox2 的另一个范围添加代码。 Combobox1 应列出“H”列中的数据,Combobox2 应列出“V”列中的数据。我不确定这样做的正确方法是什么。
-
我知道两个组合框都有相同的列表,并且列表包含“H”范围内的信息。他们也有来自“V”范围的数据吗?如果他们这样做,它可能会在列表的底部。
-
是的——“H”范围在顶部,“V”范围在两个组合框的底部。我正在尝试对找到的代码进行逆向工程,但到目前为止还没有运气。