1

正如标题所说,这是否可能以及如何?

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

4

2 回答 2

0

是的,你可以这么做。看这个例子

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
于 2012-07-03T13:46:47.663 回答
0

此代码用于更新单元格以间接引用其他工作表,它查找具有特定字符串的单元格,并更新这些单元格的值。(fsstring 存储第一个查找地址的地址,用于停止条件)。

Sub Update_Sheet_Reference()
Dim sCell As Variant, fsCell As Variant
sString = "_SearchText"
sCell = Range("A1").Address

While True
sCell = Cells.Find(What:=sString, After:=Range(sCell), LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Address
        
Range(sCell).Value = Replace(Left(Range(sCell).Value, Len(Range(sCell).Value) - 1), sString, "") + sString + "!"
If fsCell = "" Then
    fsCell = sCell
ElseIf sCell = fsCell Then Exit Sub
End If
Wend
End Sub
于 2021-10-13T13:51:45.560 回答