0

I want to search in a column and find a string. However, there are several cells with that string and I want to return an array that contains all row position.

Dim r As Range
Set r = Sheets("Sheet3").columns(3).Find(What:="TEST", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)

this return only the first row, but not all. How can I return all of rows that contain "TEST"? Thanks.

1
  • Is that the final output or are you going to use those references to return something else in the end? Commented Jul 16, 2018 at 17:40

3 Answers 3

2

This should do the trick...

It will loop through Column 3 of Sheet3 to produce an array containing the row numbers of every occurrence of TEXT. Based on the options used in your example, it is case sensitive and must occupy the whole cell.

Sub demo_FindIntoArray()
    Const searchFor = "TEST" 'case sensitive whole-cell search term
    Const wsName = "Sheet3" 'worksheet name to search
    Const colNum = 3 'column# to search
    Dim r As Range, firstAddress As String, strTxt As String, arrRows
    With Sheets("Sheet3").Columns(3)
        Set r = .Find(searchFor, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
        If Not r Is Nothing Then
            firstAddress = r.Address
            Do
                If strTxt <> "" Then strTxt = strTxt & ","
                strTxt = strTxt & r.Row
                Set r = .FindNext(r)
            Loop While Not r Is Nothing And r.Address <> firstAddress
        End If
    End With

    If strTxt <> "" Then
        arrRows = Split(strTxt,",")
        MsgBox "Found " & UBound(arrRows)+1 & " occurrences of '" & searchFor & "':" & vbLf & vbLf & strTxt
    Else
        MsgBox "'" & searchFor & "' was not found."
    End If

    '[arrRows] is now an array containing row numbers

End Sub

It works by first building a string with a comma separated list of values, and then using the Split function to split it into an array.

Sign up to request clarification or add additional context in comments.

Comments

0

This will unionize the ranges that equal "Test",

Sub SelectA1()
    Dim FrstRng As Range
    Dim UnionRng As Range
    Dim c As Range
    Set FrstRng = Range("C:C").SpecialCells(xlCellTypeConstants, 23)

    For Each c In FrstRng.Cells
        If LCase(c) = "test" Then
            If Not UnionRng Is Nothing Then
                Set UnionRng = Union(UnionRng, c)    'adds to the range
            Else
                Set UnionRng = c
            End If
        End If
    Next c

    UnionRng.Select    ' or whatever you want to do with it.
End Sub

Comments

0

You don't indicate what you want to do with the results. However, you can return an array of the row numbers containing the word "TEST" with a worksheet formula:

Case Insensitive:

=AGGREGATE(15,6,SEARCH("TEST",$C:$C)*ROW($C:$C),ROW(INDIRECT("1:" & COUNTIF($C:$C,"*TEST*"))))

Case Sensitive:

=AGGREGATE(15,6,FIND("TEST",$C:$C)*ROW($C:$C),ROW(INDIRECT("1:" & SUMPRODUCT(--ISNUMBER(FIND("TEST",$C:$C))))))

Case Insensitive; cell = test (eg entire cell contents):

=AGGREGATE(15,6,1/($C:$C="test")*ROW($C:$C),ROW(INDIRECT("1:"&COUNTIF($C:$C,"test"))))

If you want to use this array for something else, such as to return the contents of the rows where the third column = "test", you could also do this with a filter, either in VBA or on the worksheet.

There are many different appropriate solutions, depending on what you are going to do with the results of these row numbers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.