Count occurances of character string in a worksheet

vegasguy

New Member
Joined
Feb 26, 2016
Messages
6
Hi All,

How can I count the number of times a word appears in a worksheet using VBA?

Thanks in advance,

vegasguy :)
 

DiscoPistol

Active Member
Joined
Jun 6, 2006
Messages
261
Hi,

This will let you enter the word and then count how many cells it appears it. I've set it using a Range (albeit only 3 cells!)

However if a word appears in a cell twice it will only count is once.

It should also be case insensitive.

Hope that helps

DP


Code:
Sub test()

Dim Search_word As String
Dim rng As Range
Dim Word_Count As Long

Word_Count = 0

Set rng = Sheets("Sheet1").Range("A1:A3")

Search_word = InputBox("Please enter the word that you are looking for")

For Each Cell In rng.Cells
     If InStr(1, LCase(Cell.Value), LCase(Search_word)) > 0 Then Word_Count = Word_Count + 1
Next Cell

MsgBox Word_Count

End Sub
 

oldbrewer

Board Regular
Joined
Apr 11, 2010
Messages
10,990
applepearlime
limeorangemango
pineapplelimeapricotassume A1:C4 is your entire worksheet
bananacoconutlime
how many times does "lime" occur ?
this macro puts the 4 into cell A10
4For j = 1 To 4
For k = 1 To 4
If Cells(j, k) = "lime" Then Sum = Sum + 1
Next k
Next j
Cells(10, 1) = Sum
End Sub

<colgroup><col width="64" span="11" style="width:48pt"> </colgroup><tbody>
</tbody>
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,755
Hi
Welcome to the board

A simple way is to use CountIf().
Another way is to use the Find method of the Range object.
 
Last edited:

Gary McMaster

Well-known Member
Joined
Feb 8, 2009
Messages
1,977
If you click "Find All" in the "Find and Replace" dialog it will report how many different cells contain the text but will not count multiple instances in a single cell.
 

Forum statistics

Threads
1,086,257
Messages
5,388,714
Members
402,137
Latest member
pkulkarni

Some videos you may like

This Week's Hot Topics

Top