嘿,我写过这个宏,它编译但dosnt达到目标
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列中选择没有重复的值,并将其放入组合框中。问题出在第2部分。 如果列E上的值是我在combobox1中选择的值,我想选择C列中的值没有重复。 你好吗
答案 0 :(得分:0)
对于userform initialize:
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