Home » excel » excel – VBA countif statement only returns 0

excel – VBA countif statement only returns 0

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m working on a macro that is supposed to count the number of times the term “GM” appears in a column. I decided to use a countif statement, as I have before and it worked well. However, for some reason when I run my code it outputs 0 every time, which definitely is not correct. I’ve run this same code with other columns and strings and it has worked fine, but for some reason if I search this certain column for the term “GM” it fails. The only thing I can think of is maybe countif only works if the string you’re searching for is the only string in a cell, because in all cases where this is true the code works fine. In this particular case the string I’m looking for is not the only string in the cell and the code is failing. I’ve tried to find more info on whether or not this is true but I can’t find anything online. Here’s the code if anyone would like to take a look:

Function OemRequest() As Long 

Sheets("CS-CRM Raw Data").Select
Sheets("CS-CRM Raw Data").Unprotect

    Dim oem As Long
    Dim LastRow As Long
    Dim LastColumn As Long

        'Determines size of table in document
        LastRow = Range("A" & Rows.Count).End(xlUp).row
        LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column

        oem = Application.WorksheetFunction.CountIf(Range(2 & "2:" & 2 & LastRow), "gm") 

OemRequest = oem

End Function
How to&Answers:

You are correct that the COUNTIF as written will only match cells where the whole content is “gm”. The criteria in the COUNTIF function will also accept wildcards, so to match on cells that contain “gm” do:

.CountIf(Range(2 & "2:" & 2 & LastRow), "*gm*") 

Update

As you noted there is also an issue with your Range call. As it is, the expression inside the parens will evaluate to "22:2<LastRow>" (where <LastRow> is the value of the LastRow variable).

The 2‘s in there should be a variable containing the column name you’re interested in. Something like:

Dim col as String
col = "B"
... Range(col & "2:" & col & LastRow) ...

This will evaluate to "B2:B<LastRow>", which is what you want.

Answer:

Another possibility:

oem = WorksheetFunction.CountIf(Columns(LastColumn).Cells(2).Resize(rowsize:=LastRow - 1), "gm")

This will count cells containing "gm" (use wilcards if needed) in the LAST column of the table, except the one in the first row. (It assumes the table upper left corner is in cell "A1")

Of course you can create a variable if you would like to count any other column:

Dim lngCol as Long
lngCol = ...
oem = WorksheetFunction.CountIf(Columns(lngCol).Cells(2).Resize(rowsize:=LastRow - 1), "gm")

Answer:

I think in this way

Sub Main()
    Application.ScreenUpdating = 0
    Dim Count As Double
    Range("C1").Activate 'Firs row in the column
    Do While ActiveCell.Value <> ""
        If InStr(ActiveCell.Value, "MyText") Then
            Count = Count + 1
        End If
        ActiveCell.Offset(1, 0).Activate
    Loop
    Application.ScreenUpdating = 1
End Sub

This will work, only if the data cell is not empty, if there is an empty space in middle of the worksheet, do this:

Sub Main()
    Application.ScreenUpdating = 0
    Dim Count As Double
    Range("C1").Activate
    Do While ActiveCell.Row <> Rows.Count ' This wil evaluate all the rows in the 'C' Column
        If InStr(ActiveCell.Value, "MyText") Then
            Count = Count + 1
        End If
        ActiveCell.Offset(1, 0).Activate
    Loop
    Application.ScreenUpdating = 1
End Sub

Hope it’s work for you.