1

As the title say is it possible and how?

I have found a .Find function to search a column for the values I want, is it then possible to save all the addresses in an array?

The code looks like this:

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

As for now I have a MsgBox just to show the results. The idea was to store the result in an array if possible.

Community
  • 1
  • 1
Andreas
  • 49
  • 2
  • 9

2 Answers2

0

Yes you can do that. See this example

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

You can then later loop through the array to show the results

For i = LBound(MyResults) To UBound(MyResults)
    Debug.Print MyResults(i)
Next i
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
0

this code is used to update cells for indirect reference to other sheets, it finds cells with specific string, and update these cells value. (the fsstring stores the address of first find address, used for stop condition).

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
nir
  • 109
  • 1
  • 5