Author: Oscar Cronquist Article last updated on March 27, 2020

count text in formulas vba

I will in this article demonstrate a macro that counts how many times a specific text string is found in cells containing formulas.

Press with left mouse button on button named "Start" to run the macro named "CountTextInFormulas". The macro will use the value in cell C2 to count how many times that particular string is found in a worksheet. 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 worksheet. 18 is the result. There are 18 formulas in cell range F7:F24 containing text string "COUNTIF(".

Note that this macro counts all matching text string in a cell, in other words, if the text string is found three times in one cell the macro returns 3 for that cell and then returns the total to cell C3.

I will also in this article demonstrate how to

  • create a button and place it on a worksheet.
  • assign a macro to a button.
  • where to put the VBA code.
  • how to save your workbook as a macro-enabled workbook.
  • explain how the macro works.

Rahul Jadhav asks:

I have many excel files with multiple sheets and each excel sheet has many formula which are starting from particular word e.g. FDS, FDSB, etc. Some formula has FDS, FDSB occur in the middle of the formula.

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")

I need to find out how many times FDS,FDSB has been appear in the sheet(total count) 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 particular worksheet

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.

'Name macro
Sub CountTextInFormulas()

'Dimension variables and declare data types
Dim Rng As Range, C As Range, Cnt As Double, Scnt As Double

'Enable error handling
On Error Resume Next

'Save a reference to cells containing formulas to range object named Rng
Set Rng = Cells.SpecialCells(xlFormulas)

'Disable error handling
On Error GoTo 0

'Check if Rng is not empty
If Not (Rng Is Nothing) Then

   'Iterate through each cell in range object Rng
   For Each C In Rng

      'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property.
      With Worksheets("Sheet1")

          'Calculate how many times the text strign is found in a given cell
          Cnt = (Len(C.Formula) - Len(Replace(C.Formula, .Range("C2"), ""))) / Len(.Range("C2"))

       End With

       'Add number to count variable
       Scnt = Scnt + Cnt

   'Continue with next cell
   Next
    
End If

'Return number to cell C3 on worksheet Sheet1
Worksheets("Sheet1").Range("C3").Value = Scnt
End Sub

Where to put the code?

count text in formulas vba1

  1. Press shortcut keys Alt + F11 to open the Visual Basic Editor.
  2. Press with left mouse button on "Insert" on the menu.
  3. Press with left mouse button on "Module".
  4. Paste code to module window.
  5. Exit VB Editor and return to Excel.
Note, make sure you save the workbook with file extension *.xlsm (macro-enabled workbook) to keep the code attached to your file.

Create button

Add button and assign macro3

  1. Go to tab "Developer" on the ribbon. You have to enable the "Developer" tab if it is missing.
  2. Press with left mouse button on "Insert Controls" button.
  3. Insert a button (Form Controls) on the sheet near cell C2.
  4. Select a macro to assign a macro to your button.
  5. Press with left mouse button on OK to close the dialog box.

Edit text in button

Count text string in all formulas in a worksheet edit button text

  1. Press with right mouse button on on button.
  2. Press with mouse on "Edit text"
  3. A prompt appears inside the button. Clear the text and then type what you want it to say.
  4. Press with left mouse button on somewhere outside the button to save the text you wrote.

Change button size

Resize button align to cell grid

 

It is hard to select the button if a macro is assigned to it. Use the right mouse button to select without triggering the macro. The image above shows sizing handles around the button.

  1. Press with right mouse button on on the button to select it. You know it is selected when the sizing handles appear.
  2. Press and hold with the left mouse button on any of the size handles.
  3. Drag with mouse to change the size.
  4. Release the mouse button when you are happy with the button size.

You can use the SHIFT key to resize the button keeping the relationship between width and height. This works only if you press with left mouse button on and drag the corner handles.

The Alt key lets you snap to the cell grid while resizing the button, the animated image above shows this.

Change button location

Count text string in all formulas in a worksheet resize button

  1. Select the button.
  2. Press and hold with left mouse button on the button.
  3. Drag with mouse to the desired location.

You can press and hold the SHIFT key to move the button vertically or horizontally using the mouse.

The Alt key lets you snap the button to the cell grid beneath.

Get the Excel file


Count-text-in-formulasv2.xlsm