Space symbols in a "like" statement in VBA

I'm trying to use a similar function to distinguish between cells that start (other than space characters) or with 2 or 3 numeric digits followed by more space characters, but it seems to be having trouble identifying the latter. For example, for two cells containing

11 some text

and the other containing

111 some text

I am trying to write an if statement that is true for the first but not the second. I tried

if cells(i,1) like "*##[ ]*" then

      

and

if cells(i,1) like "*##\s*" then

      

and

if cells(i,1) like "*##[^#]*" then

      

See the following information on using regexes from various sources (from the first two I tried to identify 2 digits followed by a space and a third digit followed by a digit).

This is part of the for loop, and as in the examples above, the only numeric digits are at the beginning of the line, apart from whitespace. In the first code example, the if statement has a meaning for both 2 and 3 numeric digits, and for the second and third, none. I don't get it, considering what I read about regex and similar function.

I am very grateful for the guidance. I expected this to be relatively straightforward, and so I am sure I am missing something obvious and apologize if this is the case.

+3


source to share


2 answers


VBA type operator does not support RegEx. Instead, it has its own format. Spaces are matched against a literal value that does not require escaping.

Input               Op      Pattern     Result
"11 Some Text"      LIKE    "## *"      True
"11Some Text"       LIKE    "## *"      False  

      



See Microsoft documentation for details .

If you'd rather use RegEx take a look at this answer . @PortlandRunner has kindly taken the time to create a great tutorial that includes many examples.

+7


source


I read the stuff on this MSDN and it seems to work for me.



If Cells(i, 1).Value Like "## *" Then
    Debug.Print ("Match")
ElseIf Cells(i, 1).Value Like "### *" Then
    Debug.Print ("Match")
End If

      

+1


source







All Articles