使用ComboBox选择查看电子表格上的多个列以匹配条件并填充ListBox

时间:2013-10-09 20:45:11

标签: excel vba excel-vba lookup

这是我想要做的事情的视觉

ConductorSize ComboBox Value = 1
ConductorClass ComboBox Value = C

CondSize    CondClass   CondStrand
 1000           B           61
 250            B           37
  1             B           19
  8             B           7
 1000           C           91
 250            C           61
  1             C           37
  8             C           19

我希望能够使用ComboBox选择的内容来匹配ColdSize和CondClass列中的值,并使用此信息,找到Column CondStrand中匹配的值。 CondStrand将填充到同一用户窗体中的ListBox(CondStrand)中。所以列表框的答案是37

任何指针?

1 个答案:

答案 0 :(得分:0)

下面看看......

我制作了一个示例工作簿和UserForm,以便在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

将以下代码放入UserForm模块

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调用)只列出列表框中的唯一值。

您可能需要修改代码以适应您的环境,但这应该提供一个良好的起点和一些方向。

如果某些内容无效或我需要修改某些内容,请告知我们。谢谢:)