0

嘿,我已经写了这个宏,它可以编译但没有达到它的目标

Private Sub UserForm_Initialize()
Dim r As Range
With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For Each r In Sheets("Ticket").Range("e2:e200")
        If (Not IsEmpty(r.Value)) * (Not .exists(r.Value)) Then
            Me.Trade.AddItem r.Value
            .Add r.Value, Nothing
        End If
    Next
End With

Dim s As Range
Dim t As Range
With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For Each s In Sheets("Ticket").Range("c2:c200")
    For Each t In Sheets("Ticket").Range("e2:e200")
        If (Not IsEmpty(s.Value)) * (Not .exists(s.Value)) And t.Value = UCase(Trade.Value) Then
            Me.Client.AddItem s.Value
            .Add s.Value, Nothing
         End If
        Next
        Next
End With

End Sub

第一部分工作完美,它从 E 列中选择没有重复的值并将其放入组合框中。问题在第二部分。如果列 E 上的值是我在组合框 1 中选择的值,我想选择不重复列 C 中的值。谢谢你

4

1 回答 1

0

对于用户窗体初始化:

Private Sub UserForm_Initialize()

    Dim varItem As Variant
    Dim strUnq As String

    For Each varItem In Sheets("Ticket").Range("E2:E200").Value
        If Len(varItem) > 0 And InStr(1, "|" & strUnq & "|", "|" & varItem & "|", vbTextCompare) = 0 Then strUnq = strUnq & "|" & varItem
    Next varItem

    If Len(strUnq) > 0 Then Me.Trade.List = Split(Mid(strUnq, 2), "|")

End Sub

对于 Trade_Change 事件:

Private Sub Trade_Change()

    Dim rngFound As Range
    Dim strFirst As String
    Dim strTemp As String
    Dim strUnq As String

    Me.Client.Clear
    If Me.Trade.ListIndex = -1 Then Exit Sub    'Nothing selected

    With Sheets("Ticket").Range("E2:E200")
        Set rngFound = .Find(Me.Trade.Text, .Cells(.Cells.Count), xlValues, xlWhole)
        If Not rngFound Is Nothing Then
            strFirst = rngFound.Address
            Do
                strTemp = .Parent.Cells(rngFound.Row, "C").Text
                If Len(strTemp) > 0 And InStr(1, "|" & strUnq & "|", "|" & strTemp & "|", vbTextCompare) = 0 Then strUnq = strUnq & "|" & strTemp
                Set rngFound = .Find(Me.Trade.Text, rngFound, xlValues, xlWhole)
            Loop While rngFound.Address <> strFirst
            Set rngFound = Nothing
        End If
    End With

    If Len(strUnq) > 0 Then Me.Client.List = Split(Mid(strUnq, 2), "|")

End Sub
于 2013-08-20T19:22:38.900 回答