首先,我感谢任何人可以提供的任何帮助。我正在编写一个宏,它将为用户提供一个输入数字键的表单。该表单将在电子表格中搜索密钥并返回附加到该密钥的相应名称。每个键的数据可能有多个名称,并且会因键而异。我想用 .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