tags:

views:

51

answers:

2
+2  Q: 

VBA Regex issue

Hey guys,

has VBA any good mechanism for checking, if the content of a given Excel Cell matches a specific regex?

In my case i want to know, if some cell has the format

m
m2
m1234

In fact, there's just one defined letter at the beginning, followed by a not specified amount of numbers.

How do I put this into an If-Else construct?

If Doc.Cells(1,1).Value ..... ???

greets, poeschlorn

A: 

I don't know VBA, but the regex [a-zA-Z][0-9]* might be able to match what you want.

murgatroid99
Hi, seems nice, but he only recognizes "m2" or "m10", nor "m" any more...
poeschlorn
It should match `m`.
Callum Rogers
in a strange way not... [mM][0-9]* only gives m12, m1, m10
poeschlorn
+4  A: 

You can get at the VBScript RegExp objects via Tools->References & adding "Microsoft VBScript Regular Expressions 5.5"

Alternatively a quick way to do it, if you don't need to check for a subsequent letter as in `m1234X1 is:

if Doc.Cells(1,1).Value like "[a-zA-Z]#*" then ...

(This doesn't require a reference to anything)

Alex K.
@Alex I didn't VB could do RegEx. That's great to know.
Ben McCormack
just another question...how do i check, if my string matches a regex like "abc_123" or "abc123" (where "abc" is always at the beginning?)
poeschlorn