まず、どなたでもご協力いただければ幸いです。ユーザーに数字キーを入力するためのフォームを提供するマクロを作成しています。フォームはスプレッドシートでキーを検索し、そのキーに関連付けられた対応する名前を返します。データはキーごとに複数の名前を持つことができ、キーによって異なります。.Find と .FindNext を使用してデータをループし、そのキーに関連付けられているすべての可能な名前を見つけたい (私はこの部分を達成しました)。私が問題を抱えている部分は、ループ中に、別のサブに渡すことができる配列に各名前を格納することです。ユーザーが別のコマンド ボタンをクリックして、可能な名前を選択する前に循環できるように、配列を渡したいと思います。
Private Sub FindNameButton_Click()
Dim KeyMatch As Long
Dim NameRow As Long
FindName As Range
KeyMatch = KeyBox.Value ' The UserForm input box
With Worksheets("Master List"). Range("D:D")
Set FindName = .Find(What:= KeyMatch, LookAt:= xlWhole, LookIn:= xlValues, MatchCase:= False)
If not FindName Is Nothing Then
FirstAddress = FindName.Address
Do
Application.GoTo FindName
NameRow = ActiveCell.Row
Cells(NameRow, 2).Select 'Selects the name associated with the key identifier
NameBox.Value = ActiveCell.Value 'Fills the UserForm box with the name
' I would like to fill the array here with each name is it passes through but I have no idea how
NameArray(i) = ActiveCell.Value ' ??????
Set FindName = .FindNext(FindName)
Loop While FindName is Nothing and FristAddress <> FindName.Address
End With
End Sub
Private Sub NextNameButton_Click()
Static cnt As Long
If cnt <= Ubound(NameArray) Then
NameBox.Value = NameArray(cnt) 'Fill UserForm Name Box with name from Name Array
Else
cnt = 0
End If
cnt = cnt + 1 ' increase every time button is clicked
End Sub