正如标题所说的那样可能以及如何?
我找到了.Find
函数来搜索列中我想要的值,是否可以将所有地址保存在数组中?
代码如下所示:
Set wsRaw = Worksheets("raw_list")
Set oRange = wsRaw.Columns(PhaseCol)
SearchString = "control"
Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
FoundAt = aCell.Address
Do While ExitLoop = False
Set aCell = oRange.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
FoundAt = FoundAt & ", " & aCell.Address
Else
ExitLoop = True
End If
Loop
Else
MsgBox SearchString & " not Found"
End If
MsgBox "The Search String has been found these locations: " & FoundAt
Exit Sub
至于现在,我只有MsgBox
才能显示结果。我的想法是尽可能将结果存储在数组中。
答案 0 :(得分:1)
是的,你可以这样做。见这个例子
Dim MyResults() As String
Dim n As Long
n = 1
'
'~~> rest of the code
'
If Not aCell Is Nothing Then
Set bCell = aCell
ReDim Preserve MyResults(n)
MyResults(n) = aCell.Address
n = n + 1
Do While ExitLoop = False
Set aCell = oRange.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
ReDim Preserve MyResults(n)
MyResults(n) = aCell.Address
n = n + 1
Else
ExitLoop = True
End If
Loop
Else
MsgBox SearchString & " not Found"
End If
然后,您可以稍后循环遍历数组以显示结果
For i = LBound(MyResults) To UBound(MyResults)
Debug.Print MyResults(i)
Next i