0

这是我正在尝试做的事情的视觉效果

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 Selections 的内容来匹配 Columns CondSize 和 CondClass 中的值,并使用此信息找到将在 Column CondStrand 中匹配的值。CondStrand 将被填充到同一用户窗体中的 ListBox (CondStrand) 中。所以列表框的答案是 37

任何指针?

4

1 回答 1

0

下面给大家看看...

我制作了一个示例工作簿和用户窗体以在 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 调用)将仅列出列表框中的唯一值。

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

请让我知道是否有问题或我需要修改某些内容。谢谢 :)

于 2013-10-09T23:31:33.997 回答