Home » excel » How do I match a single double quote (") using excel vba's Like function?

How do I match a single double quote (") using excel vba's Like function?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m using excel’s vba to match some numerical ratings. The rating can include a suffix:

  1. + add .25 to the rating
  2. subtract .25 from the rating
  3. add .50 to the rating

So for example if the rating: 5″ would really mean 5.5 or 5+ would mean 5.25

I have a simple conditional:

if ActiveCell.Characters(i,1).Text Like [123456789+-"] then ...

Unfortunately the ” produces an invalid character error. I’ve tried escaping as follows:

  • “””
  • ‘”‘
  • \”
  • chr(34)

but can’t seem to get the code to work. Thus, my question: How do you match a single double quote using excel VBA’s Like function.

How to&Answers:

Use double quotes around Like, i.e.:

...Like "[-123456789+""]"...

Basically, when using Like, the regex is wrapped in double quotes and the double quote inside it is escaped with a second one.

Note that the - (hyphen) needs to be at the start or end of the regex, otherwise it denotes a range of characters. Thanks @ssarabando !