## Count text strings in formulas (vba)

*I have many excel files with multiple sheets and each excel sheet*

* has many formula which are starting from perticular word e.g. FDS, FDSB, etc some formula has FDS, FDSB occur in the middle of the formula.*

* i need to find out how many times FDS,FDSB has been appear in the sheet(total count)*

* below is the formulae for your referance*

* =IFERROR(FDSB($D21, "IC_ESTIMATE_DATE(ALL, EXP_RPT,QTR, 3, 0, , , 'MM/DD/YYYY')@RC_ESTIMATE_DATE(ALL, EXP_RPT, QTR, +3, 0, , , 'MM/DD/YYYY')"), "na")*

*in such a way there are many formulas in all the cells*

* i just need to count the how many times FDSB occur in one perticular sheet*

The search string is in cell C2. Click the "start" box and the result is shown in cell c3.

In the example above, the macro counts how many times the text string "COUNTIF(" is found on the current sheet. 18 is the result. (There are 18 formulas in cell range F7:F24.)

### VBA Code

I wanted to quickly select all cells containing formulas on a sheet. I found Rick Rothstein (MVP - Excel) macro here and modified it.

Sub CountTextInFormulas() Dim Rng As Range, C As Range, Cnt As Double, Scnt As Double On Error Resume Next Set Rng = Cells.SpecialCells(xlFormulas) On Error GoTo 0 If Not (Rng Is Nothing) Then For Each C In Rng With Worksheets("Sheet1") Cnt = (Len(C.Formula) - Len(Replace(C.Formula, .Range("C2"), ""))) / Len(.Range("C2")) End With Scnt = Scnt + Cnt Next End If Worksheets("Sheet1").Range("C3").Value = Scnt End Sub

**Instructions**

- Go to the VB Editor (Alt+ F11)
- Click "Insert"
- Click "Module"
- Paste code to module
- Exit VB Editor

**Create button**

- Go to tab "Developer"
- Click "Insert Controls" button
- Insert a button (Form Controls) on the sheet near cell C2.

**Assign button**

- Right click on your new button
- Click "Assign Macro..."
- Select CountTextInFormulas
- Click OK

### Download excel *.xlsm file

### Functions in this post:

**COUNTIF(***range*, *criteria***)
**Counts the number of cells within a range that meet a single criterion that you specify.

### Category: Vba

This post describes how to copy values between sheets. I am using the invoice template sheet. This macro copies rows […]

Comments(40) Filed in category: Excel, VBA

This post describes how to copy selected rows using checkboxes. In a previous post I showed you how to add/remove […]

Comments(30) Filed in category: Check-boxes, Excel, VBA

UPDATE: It is not possible to count conditionally formatted cells using vba as far as I know, I recommend you […]

Comments(26) Filed in category: Conditional formatting, Count values, Excel, User defined functions (udf), VBA

Table of contents Missing numbers (array formula) Missing numbers (vba) Missing numbers (array formula) Question: I want to find missing […]

Comments(24) Filed in category: Excel, VBA

Robert Jr asks: Oscar, I am using the VBA code & FilterUniqueSort array to generate unique lists that drive Selection […]

Comments(17) Filed in category: Excel, Sort values, Unique distinct values, User defined functions (udf), VBA

You can quickly run a macro using a drop down list. The drop down list may contain multiple macro names. […]

Comments(17) Filed in category: Drop down lists, Excel, VBA

This user defined function creates an unique list of words and their frequency in selected range. User defined function: =FreqWords(cell_range, […]

Comments(16) Filed in category: Count values, Excel, Frequency, User defined functions (udf), VBA

Let me show you how to create self adjusting columns in excel with a few lines of vba code. Example, […]

Comments(16) Filed in category: Excel, VBA

Rahul asks: i want to know that when we create a vlookup sheet, and in the name column we enter […]

Comments(16) Filed in category: Excel, VBA

Question: hi all, thanks for the great formula/array formula. it works great. lately, i noticed that the array formula will […]

Comments(14) Filed in category: Advanced filter, Excel, Unique distinct values, VBA

### 4 Responses to “Count text strings in formulas (vba)”

### Leave a Reply

**How to add a formula to your comment:**

<code>your formula</code>

**How to add VBA code to your comment:**

[vb 1="vbnet" language=","]

VBA code

[/vb]

**How to add a picture to your comment:**

Upload picture to postimage.org

Add picture link to comment.

**Contact Oscar**

You can contact me through this webpage

I have some comments about your macro's code...

1) You declate a clen variable and set it in the first line of code inside the For..Next loop, but then you never use it afterwards.

2) All the variable you declared as Single will be whole numbers, so I think declaring them as Long would be more appropriate.

3) You protected the call to SpecialCells from erroring out if the worksheet has no formulas on it; however, the For statement will error out if the Rng variable is Nothing (which would be the case when the sheet has no formulas on it).

Below is shorter macro which (untested claim) should execute faster than the code you posted..

I presume you chose to implement your code as a macro rather than as UDF (user defined function) because your code makes use of the SpecialCells function which, for whatever reason, Microsoft chose to exclude its use within UDFs. However, the approach my code uses does not have such a restriction, so it can be converted into a UDF quite easily...

So, instead of maintaining a button and having to click it whenever you change the text being counted OR number of formulas on the sheet, you can just have this formula (which uses C2 for the text string to count... a quoted text string could be used as well) which should update automatically...

=FCount(C2)

Note that, for the UDF, I also provided the ability to perform a case sensitive or case insensitive search (should the user want to count, say, text strings within quoted text inside of formulas only) via an optional second argument. The default (if this second argument is omitted) is FALSE which performs a case insensitive search; specifying TRUE (or then number 1 if you prefer short formulas) will performa a case sensitive search...

=FCount(C2,TRUE)

Rick Rothstein (MVP - Excel),

1, Corrected!

2, Well, there can´t be anything else than whole numbers? What if the macro returned 0.5, what does that mean? The text string was found partially?

3, Corrected!

I presume you chose to implement your code as a macro rather than as UDF (user defined function) because your code makes use of the SpecialCells function which, for whatever reason, Microsoft chose to exclude its use within UDFs. However, the approach my code uses does not have such a restriction, so it can be converted into a UDF quite easily...You are right!

Both the subroutine and the function work, many thanks for sharing!

With respect to your response #2:

This line of code...

can only assign whole number values to the Cnt variable. The subtraction part will always leave an exact multiple of the length of the text in cell C2 (since that is how many character the Replace function removed... the multiple number of times the text in cell C2 occurred in the original text), so that when you divide it by the length of the text in cell C2, the result must always be a whole number value... fractional results can never result from the calculation. Hence, it would be more appropriate to declare Cnt as a Long instead of a Single. And, since the Scnt variable is calculated by adding all of these resulting Cnt values, it too can only contain whole number values, so would be more appropriate for it to be declared as a Long instead of a Single as well.

Rick Rothstein (MVP - Excel),

Thanks for explaining!