How do I count all instances of a certain text string in a sheet

%3CLINGO-SUB%20id%3D%22lingo-sub-2099098%22%20slang%3D%22en-US%22%3EHow%20do%20I%20count%20all%20instances%20of%20a%20certain%20text%20string%20in%20a%20sheet%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2099098%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3EIn%20VBA%20I%20need%20to%20change%20all%20instances%20of%20a%20certain%20text%20value%20in%20a%20workbook%20to%20a%20new%20value%2C%20but%20I%20also%20need%20to%20know%20the%20number%20of%20changes%20made%20(instances%20of%20text%20at%20start).%20I%20have%20done%20some%20searching%20which%20talked%20about%20the%20CountIF%20function%20and%20I%20have%20the%20following%20code.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3EDim%20objSheet%20As%20Worksheet%0ADim%20lngChanges%20As%20Long%0ADim%20rngAllCells%20As%20Range%0A%20%20%20%20lngChanges%20%3D%200%0A%20%20%20%20For%20Each%20objSheet%20In%20ActiveWorkbook.Worksheets%0A%20%20%20%20%20%20%20%20Set%20rngAllCells%20%3D%20Worksheets(objSheet.Name).Cells%0A%20%20%20%20%20%20%20%20lngChanges%20%3D%20lngChanges%20%2B%20WorksheetFunction.CountIf(rngAllCells%2C%20%22*My%20Old%20Text*%22)%0A%20%20%20%20%20%20%20%20objSheet.Cells.Replace%20What%3A%3D%22My%20Old%20Text%22%2C%20Replacement%3A%3D%22My%20New%20Text%22%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20LookAt%3A%3DxlPart%2C%20SearchOrder%3A%3DxlByRows%2C%20MatchCase%3A%3DFalse%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20SearchFormat%3A%3DFalse%2C%20ReplaceFormat%3A%3DFalse%0A%20%20%20%20Next%20objSheet%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3ENow%20it%20does%20count%20all%20text%20cells%20that%20contain%20my%20specified%20value.%3C%2FP%3E%3CP%3EThe%20problem%20occurs%20if%20my%20text%20value%20is%20part%20of%20a%20formula.%3C%2FP%3E%3CP%3Ei.e.%20%3DVLOOKUP(%22My%20Old%20Text%22%2C%20.%20.%20.%20)%3C%2FP%3E%3CP%3ENow%20I%20do%20want%20the%20text%20to%20be%20replaced%20in%20formulas%20and%20the%20Replace%20command%20above%20does%20that%20nicely.%3C%2FP%3E%3CP%3EBut%20the%20CountIF%20function%20does%20not%20look%20in%20formulas%2C%20only%20in%20values%2C%20so%20the%20number%20of%20affected%20changes%20(in%20lngChanges)%20is%20incorrect.%3C%2FP%3E%3CP%3EDoes%20anyone%20have%20suggestions%20on%20how%20to%20get%20the%20number%20of%20changes%20made%20correctly%3F%3C%2FP%3E%3CP%3EThank%20You%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2099098%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2099312%22%20slang%3D%22de-DE%22%3ESubject%3A%20How%20do%20I%20count%20all%20instances%20of%20a%20certain%20text%20string%20in%20a%20sheet%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2099312%22%20slang%3D%22de-DE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F920454%22%20target%3D%22_blank%22%3E%40Gilgamesh1964%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3EMaybe%20that%20will%20help%20you%20too%2C%20I%20don't%20know%2C%20because%20I%20know%20that%20I%20don't%20know%20anything.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3ENikolino%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20searchWord%20()%0A%20%20%20%20%20Dim%20last%20line%0A%20%20%20%20%20Dim%20last%20column%0A%20%20%20%20%20Dim%20wordCounter%20As%20Integer%0A%20%20%20%20%20lastline%20%3D%20ActiveSheet.Cells.SpecialCells%20(xlCellTypeLastCell)%20.Row%0A%20%20%20%20%20last%20column%20%3D%20ActiveSheet.Cells.SpecialCells%20(xlCellTypeLastCell)%20.Column%0A%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%20For%20i%20%3D%201%20To%20last%20line%0A%20%20%20%20%20%20%20%20%20For%20ii%20%3D%201%20To%20last%20column%0A%20%20%20%20%20%20%20%20%20%20%20%20%20'Find%20the%20cell%20that%20contains%20my%20value%20and%20count%0A%20%20%20%20%20%20%20%20%20%20%20%20%20'wordCounter%20%3D%20wordCounter%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20If%20InStr%20(1%2C%20Cells%20(i%2C%20ii)%20.Value%2C%20%22text%22)%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20wordCounter%20%3D%20wordCounter%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20%20Next%0A%20%20%20%20%20Next%0A%20%20%20%20%0A%20%20%20%20%20MsgBox%20wordCounter%0AEnd%20Sub%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello,

In VBA I need to change all instances of a certain text value in a workbook to a new value, but I also need to know the number of changes made (instances of text at start). I have done some searching which talked about the CountIF function and I have the following code.

Dim objSheet As Worksheet
Dim lngChanges As Long
Dim rngAllCells As Range
    lngChanges = 0
    For Each objSheet In ActiveWorkbook.Worksheets
        Set rngAllCells = Worksheets(objSheet.Name).Cells
        lngChanges = lngChanges + WorksheetFunction.CountIf(rngAllCells, "*My Old Text*")
        objSheet.Cells.Replace What:="My Old Text", Replacement:="My New Text", _
            LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
    Next objSheet

Now it does count all text cells that contain my specified value.

The problem occurs if my text value is part of a formula.

i.e. =VLOOKUP("My Old Text", . . . )

Now I do want the text to be replaced in formulas and the Replace command above does that nicely.

But the CountIF function does not look in formulas, only in values, so the number of affected changes (in lngChanges) is incorrect.

Does anyone have suggestions on how to get the number of changes made correctly?

Thank You

2 Replies

@Gilgamesh1964 

Maybe that will help you too, I don't know, because I know that I don't know anything.

Nikolino

 

Sub searchWord ()
     Dim last line
     Dim last column
     Dim wordCounter As Integer
     lastline = ActiveSheet.Cells.SpecialCells (xlCellTypeLastCell) .Row
     last column = ActiveSheet.Cells.SpecialCells (xlCellTypeLastCell) .Column
    
    
    
     For i = 1 To last line
         For ii = 1 To last column
             'Find the cell that contains my value and count
             'wordCounter = wordCounter + 1
             If InStr (1, Cells (i, ii) .Value, "text") Then
                 wordCounter = wordCounter + 1
             Else
             End If
         Next
     Next
    
     MsgBox wordCounter
End Sub 

 

 

@Nikolino 

Thank you for your response.

After sleeping on it (actually not sleeping but tossing and turning thinking about it :-)) I decided to completely abandon my approach of CountIF and objSheet.Cells.Replace so used the following.

Private Sub MyReplace(lngLookin As Long, lngChanges As Long, rngAllCells As Range)

Dim rngCell As Range

    With rngAllCells
        Set rngCell = .Find(strFromDir, LookIn:=lngLookin)
        If Not rngCell Is Nothing Then
            Do 'While Not rngCell Is Nothing
                lngChanges = lngChanges + 1
                If lngLookin = xlValues Then rngCell.Value = Replace(rngCell.Value, strFrom , strTo )
                If lngLookin = xlFormulas Then rngCell.Formula = Replace(rngCell.Formula, strFrom, strTo )
                Set rngCell = .FindNext(rngCell)
            Loop While Not rngCell Is Nothing
        End If 'Not rngCell Is Nothing
    End With 'rngAllCells

End Sub

strFrom and strTo are global constants.

The procedure is called with the first parameter as either xlValues or xlFormulas (the builtin excel constants) to determine if it searches in values or formulas.

This seems to work