Count number of strings found

vbaNewby

Board Regular
Joined
Jan 26, 2011
Messages
138
Hello,

I currently use the find function to find a string within a spreadsheet.

1) I would like to set an integer value and increment everytime I find a string within a worksheet.

2) I would like to use a specific range to start my next find after I know the count.

I found a FindAll function online but I'm wondering if there is anything built in that I can use?

Thanks in advance.
 
If that's the case, why not look backward through the range?
Code:
Sub test()
Dim c As Range
Set c = Range("A1:A10").Find("Hello", Range("A1"), , , , xlPrevious)
If Not c Is Nothing Then MsgBox c.Address
End Sub
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Here is a generalized way to loop through all the instances of a search term in a range. Note that this does not require the use of countOfFinds (COUNTIF) , unless there is some other reason you require that information.
Code:
Sub test()
    Dim foundCell As Range
    Dim firstFoundAddress As String
    Dim countOfFinds As Long
    
    Dim searchTerm As String
    searchTerm = "hello"
    
    countOfFinds = 0
    With ThisWorkbook.Sheets("Sheet1").Range("A1:A10")
        Set foundCell = .Find(What:=searchTerm, after:=.Cells(.Rows.Count, 1), _
            LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
            
        If foundCell Is Nothing Then
            MsgBox "No " & searchTerm & " in " & .Address
            Exit Sub
        End If
        
        firstFoundAddress = foundCell.Address
        Do
            countOfFinds = countOfFinds + 1
            MsgBox searchTerm & " found in " & foundCell.Address
            Set foundCell = .FindNext(after:=foundCell)
        Loop Until firstFoundAddress = foundCell.Address
    End With
    
    MsgBox "A total of " & countOfFinds & " '" & searchTerm & "'s were found."
    
End Sub
If you want all of the cells containing "hello" to be extracted, you could either add to a collection or use Application.Union to create a discontinous range, where the line
MsgBox searchTerm & " found in " & foundCell.Address
is currently found.

Thanks Mike, I can certainly work with these to come up with something. Appreciate the help.
 
Upvote 0
If that's the case, why not look backward through the range?
Code:
Sub test()
Dim c As Range
Set c = Range("A1:A10").Find("Hello", Range("A1"), , , , xlPrevious)
If Not c Is Nothing Then MsgBox c.Address
End Sub

This is also something great to know, I'm going to work on this and let you guys know. Thanks again HOTPEPPER. :rofl:
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top