We help IT Professionals succeed at work.

Modify vba function

776 Views
Last Modified: 2012-02-14
Would like the attached code to also handle the following 3 scenarios:

1) S1@W3.com

Desired Result:
Name= ""
Address = "S1@W3.com"

2) S1.test@W3.com
Desired Result:
Name= ""
Address = "S1.test@W3.com"

3) Sam Abboushi <S1.test@W3.com>
Sam Abboushi [S1.test@W3.com]
Sam Abboushi (S1.test@W3.com)
Desired Result:
Name= "Sam Abboushi"
Address = "S1.test@W3.com"

Function splitAddy(str As String) As Variant
Dim regEx As Object
Dim matchCollection As Object

Debug.Print splitAddy("fred smith <fred@fred.com>")(1)
fred@ fred.com
Debug.Print splitAddy("fred smith <fred@fred.com>")(0)
fred smith

    Set regEx = CreateObject("vbscript.regexp")
    With regEx
        .Pattern = "([\w|-]*)[,; ] *([\w|-]*) *(?:[\[|{|(|<])([\w|-]*@[\w|-]*.[\w|-]*)(?:[\]|}|)|>])"
        .Global = True
        .IgnoreCase = True
    End With
    Set matchCollection = regEx.Execute(str)
    If matchCollection.Count = 0 Then
        splitAddy = ""
    Else
        splitAddy = Array(matchCollection(0).submatches(0) & " " & matchCollection(0).submatches(1), matchCollection(0).submatches(2))
    End If

End Function

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2011
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks-

The following don't seem to work:
Debug.Print splitAddy("fred  <fred_1.33@fred.com>")(1)

Debug.Print splitAddy("fred  <fred_1.33@fred.com>")(0)
CERTIFIED EXPERT
Top Expert 2011

Commented:
Function splitAddy(str As String) As Variant
Dim regEx As Object
Dim matchCollection As Object

'Debug.Print splitAddy("fred smith <fred@fred.com>")(1)
'fred@ fred.com
'Debug.Print splitAddy("fred smith <fred@fred.com>")(0)
'fred smith
'Debug.Print splitAddy("fred@fred.com")(1)
'Debug.Print splitAddy("fred.bloggs@fred.com")(1)

    Set regEx = CreateObject("vbscript.regexp")
    With regEx
        .Pattern = "([\w|-|_|.]*)[,; ] *([\w|-|_|.]*) *(?:[\[|{|(|<])([\w|-|_|.]*@[\w|-|_]*.[\w|-|_|.]*)(?:[\]|}|)|>])"
        .Global = True
        .IgnoreCase = True
    End With
    Set matchCollection = regEx.Execute(str)
    If matchCollection.Count = 0 Then
'        splitAddy = ""
        If InStr(str, "@") > 0 Then
            splitAddy = Array("", str)
        Else
            splitAddy = Array("", "")
        End If
    Else
        splitAddy = Array(matchCollection(0).submatches(0) & " " & matchCollection(0).submatches(1), matchCollection(0).submatches(2))
    End If

End Function

Open in new window

Author

Commented:
Thanks Chris!  Been wrestling trying to understand your regex.  Hope to figure it out one of these days ; )

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.