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 から重複なしで値を選択し、コンボボックスに入れます。問題は第2部です。列 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 に答える