Search code examples
excelexcel-2007strcmpvba

Excel VBA StrCmp sub or function not defined


I am trying to use the StrCmp function in excel vba (using excel 2007) and am getting the following error: Compile Error: Sub or Function Not Defined.

Here is my code:

    StrCmp(Worksheets(1).Range("I" & x).Value, "Critical")

Any help would be greatly appreciated.


Solution

  • The functions name is StrComp and to call it you would say :
    StrComp( StringArg1, StringArg2, compareMethod) = 0 for a match.
    It seems like in your case you would use

    if StrComp(Cstr(Worksheets(1).Range("I" & x).Value), "Critical", vbTextCompare) = 0 then
        ' you now have found a match
        ' do something
    end if
    

    More on the StrComp(), also here and also see this here on SO