下面给大家看看...
我制作了一个示例工作簿和用户窗体以在 Win7 和 Excel 2007 上进行测试
我做了几个假设:
1) Data is in Sheet1 code name Sheet1
2) Data is in Columns A, B, and C like your example
3) The Data has a Header in row 1 and Data starts on row 2
将以下代码放入您的用户窗体模块
Option Explicit
Private Sub ComboBox1_Change()
' Delete this if you want to call the routine manually
Call GetCondStrandValue
End Sub
Private Sub ComboBox2_Change()
' Delete this if you want to call the routine manually
Call GetCondStrandValue
End Sub
' To Manually call the routine
Private Sub CommandButton1_Click()
Call GetCondStrandValue_OnlyUniqueValues
End Sub
Private Sub UserForm_Initialize()
Dim RR As Range, rCell As Range
Dim objDictionary As Object
Set objDictionary = CreateObject("Scripting.Dictionary")
With Sheet1
Set RR = .Range("A2:A" & .Range("A65536").End(xlUp).Row)
With Me.ComboBox1
.Clear
For Each rCell In RR
If Not objDictionary.Exists(rCell.Value) Then
.AddItem rCell.Value
objDictionary.Add rCell.Value, objDictionary.Count
End If
Next
End With
objDictionary.RemoveAll
Set RR = .Range("B2:B" & .Range("B65536").End(xlUp).Row)
With Me.ComboBox2
.Clear
For Each rCell In RR
If Not objDictionary.Exists(rCell.Value) Then
.AddItem rCell.Value
objDictionary.Add rCell.Value, objDictionary.Count
End If
Next
End With
objDictionary.RemoveAll
Set objDictionary = Nothing
With Me.ListBox1
.Clear
End With
End With
End Sub
Private Sub GetCondStrandValue()
Dim iRow As Long
Dim strValue As String
strValue = vbNullString
If Me.ComboBox1.Value = vbNullString Or Me.ComboBox2.Value = vbNullString Then Exit Sub
With Sheet1
For iRow = 2 To .Range("A65536").End(xlUp).Row
If StrComp(.Cells(iRow, 1).Value, Me.ComboBox1.Value, 1) = 0 And _
StrComp(.Cells(iRow, 2).Value, Me.ComboBox2.Value, 1) = 0 Then
strValue = .Cells(iRow, 3).Value
Exit For
End If
Next
End With
If strValue = vbNullString Then Exit Sub
With Me.ListBox1
'If you only want a single value in the listbox un-comment the .clear line
'Otherwise, values will continue to be added
'.Clear
.AddItem strValue
.Value = strValue
.SetFocus
End With
End Sub
Private Sub GetCondStrandValue_OnlyUniqueValues()
Dim iRow As Long
Dim strValue As String
Dim objDictionary As Object
strValue = vbNullString
If Me.ComboBox1.Value = vbNullString Or Me.ComboBox2.Value = vbNullString Then Exit Sub
With Sheet1
For iRow = 2 To .Range("A65536").End(xlUp).Row
If StrComp(.Cells(iRow, 1).Value, Me.ComboBox1.Value, 1) = 0 And _
StrComp(.Cells(iRow, 2).Value, Me.ComboBox2.Value, 1) = 0 Then
strValue = .Cells(iRow, 3).Value
Exit For
End If
Next
End With
If strValue = vbNullString Then Exit Sub
Set objDictionary = CreateObject("Scripting.Dictionary")
With Me.ListBox1
For iRow = .ListCount - 1 To 0 Step -1
If Not IsNull(.List(iRow)) Then
If Not objDictionary.Exists(.List(iRow)) Then
objDictionary.Add .List(iRow), objDictionary.Count
Else
.RemoveItem iRow
End If
End If
Next
If Not objDictionary.Exists(strValue) Then
.AddItem strValue
.Value = strValue
.SetFocus
End If
End With
End Sub
UserForm Initialize 事件将基于 A 列和 B 列构建 ComboBoxes 1 和 2 的值。创建 Dictionary 对象以便仅添加唯一值。
我使用 Combox Change Event 来触发 GetCondStrandValue 例程,该例程将遍历每一行,寻找 ComboBox 1 与 A 列和 ComboBox 2 中的单元格值以及同一行中 B 列中的单元格之间的匹配项。如果找到匹配项,则将同一行 C 列中的值添加到变量中,然后将其添加到列表框...
我做了两种例程,另一个例程(在我的示例中由 CommandButton 调用)将仅列出列表框中的唯一值。
您可能需要修改代码以适应您的环境,但这应该提供一个良好的起点和一些方向。
请让我知道是否有问题或我需要修改某些内容。谢谢 :)