This is a public service message.
The other day some folks in the office were asking how to tell if a certain character appeared in a cell in Excel. I was shocked when the traditional VB function failed miserably. The call of instr(1, “blub”, “b”) worked fine from the immediate window… but =instr(1,A1,”b”) would die in a cell. Was my face red! The computer “genius” sure looked like a chump just then.
Never again, I tell you. Never again.
Allright, so you want to be able to do this:
To make it happen, hit alt-F11 to go to the code screen. Click “Insert | Module” to add a new code module. Then click”Tools | References” so you can check the “Microsoft VBScript Regular Expressions 5.5” box that will make this work:
Now paste in this code in your module and you’re good to go!
Function M(Value As String, Pattern As String, Optional IgnoreCase As Boolean = False) Dim r As New VBScript_RegExp_55.RegExp r.Pattern = Pattern r.IgnoreCase = IgnoreCase If r.Test(Value) Then M = "Matches '" & Pattern & "'" Else M = "" End If End Function Function StartsWith(Value As String, Pattern As String, Optional IgnoreCase As Boolean = False) Dim r As New VBScript_RegExp_55.RegExp r.Pattern = "^" & Pattern r.IgnoreCase = IgnoreCase If r.Test(Value) Then StartsWith = "Starts with '" & Pattern & "'" Else StartsWith = "" End If End Function Function EndsWith(Value As String, Pattern As String, Optional IgnoreCase As Boolean = False) Dim r As New VBScript_RegExp_55.RegExp r.Pattern = Pattern & "$" r.IgnoreCase = IgnoreCase If r.Test(Value) Then EndsWith = "Ends with '" & Pattern & "'" Else EndsWith = "" End If End Function Function S(Value As String, Pattern As String, ReplaceWith As String, Optional IgnoreCase As Boolean = False) Dim r As New VBScript_RegExp_55.RegExp r.Pattern = Pattern r.IgnoreCase = IgnoreCase r.Global = True S = r.Replace(Value, ReplaceWith) End Function
October 23, 2008 at 8:02 pm |
I’m not sure how you would do it, but wouldn’t M() be better if it returned a boolean? Then you could do:
=IF( M( A1, ‘b’ ), “Matches ‘b'”, “Doesn’t match ‘b'” )
Which doesn’t make your example any better, but would allow enhancements in the future.
February 3, 2009 at 11:50 pm |
Of course, you could also have just used the =search() function.
for your example, you’d find =search(“a”, B2, 1) and if it existed, it would list where it was in the cell, else return an error.
=if(iserror(search(“a”, B2, 1)),”Does not Exist”,search(“a”, B2, 1))
Would even tell you it doesn’t exist in that cell if you wanted.
May 19, 2009 at 1:17 pm |
This is excellent. Thanks!
July 2, 2009 at 3:10 am |
If I emailed a sheet containing the formula to someone, would their desktop need to have that VB regex reference installed inorder for the regex portion to still work?
March 18, 2010 at 3:23 pm |
Alo,
The problem with the search function is that it doesn’t support regular expressions like VBScript does. Regular expressions allow you to do a far more advanced search within the screen to get exactely what you want.
April 4, 2010 at 8:24 pm |
Thanks for this – it works well!
Would there be a way to include variations in spelling within the formula, like for “colour” (color|colour)? I tried but haven’t had any success.
Thanks!