PDA

View Full Version : Understanding a Function



bryVA
06-30-2009, 09:40 AM
Hello,

I am not very knowledgable about VBA and as such I am not sure how the following function works. Can someone explain how this works and what it is doing?

Option Explicit
Function ParseName(str As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.IgnoreCase = True
re.Global = True
re.Pattern = "(^\s*\w+)\s*(([\w.]*?)\s*)?(\w+)\s*(JR|SR|III|$)"
ParseName = re.Replace(str, "$4, $1 $3 $5")
ParseName = Application.WorksheetFunction.Trim(ParseName)
End Function

Thanks all,

mdmackillop
06-30-2009, 11:23 AM
RegExp is a mystery to many (including me). Have a look at this KB Item (http://www.vbaexpress.com/kb/getarticle.php?kb_id=68)

bryVA
06-30-2009, 11:34 AM
Thanks for the examples. That is really interesting. Again thanks mdackillop for helping me as you always seem to do.