• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Alternative for COUNTIF

Derrick Rikley

New Member
Hi Everyone,

I have searched for a solution to this and can't find anything anywhere.

I have the following code working in a cell and now I need to move it into a macro.

=IF(COUNTIF(X2,"*Target*"),1,IF(COUNTIF(X2,"*Critical*"),1,IF(COUNTIF(X2,"*Virtual*"),1,)))

Does anyone have any ideas?

Thanks!

D.
 
This is one way of doing it!

Code:
    If InStr(1, "targetcriticalvirtual", Range("X2").Value, vbTextCompare) > 0 Then
        MsgBox 1
    Else
        MsgBox False
    End If
 
Ah! OK. You can use the following function as a UDF in Excel. First you need to paste this in a code module (insert a new module just to be on the safer side)

Code:
Function CHECKINSTRING(strVal As String) As Long

    If InStr(1, "targetcriticalvirtual", strVal, vbTextCompare) > 0 Then
        CHECKINSTRING = 1
    End If
    
End Function
Then in the Cell of the corresponding row, use this as a function =CHECKINSTRING(X2)
 
Hi, Derrick Rikley!
I agree with shrivallabha but if despite of that you still need to do it by VBA, you can call the original Excel function from VBA using the Application.WorksheetFunction object:
=IF(Application.WorksheetFunction.Countif...
Regards!
 
Back
Top