Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
EXCEL VBA在数组中存储搜索结果?_Excel_Vba - Fatal编程技术网

EXCEL VBA在数组中存储搜索结果?

EXCEL VBA在数组中存储搜索结果?,excel,vba,Excel,Vba,正如标题所说,这可能吗?如何可能 我找到了一个.Find函数来搜索列中我想要的值,那么是否可以将所有地址保存在一个数组中 代码如下所示: Set wsRaw = Worksheets("raw_list") Set oRange = wsRaw.Columns(PhaseCol) SearchString = "control" Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _ LookAt

正如标题所说,这可能吗?如何可能

我找到了一个
.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
来显示结果。其想法是尽可能将结果存储在一个数组中。

是的,您可以这样做。看这个例子

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

是的,你能做到。看这个例子

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

非常感谢您的快速回复!考虑到你从我的博客中获得了代码,这很容易:谢谢你的快速回复!考虑到您从我的博客中获得了代码,这很容易