Split Phone and Email

austinandreikurt

Board Regular
Joined
Aug 25, 2020
Messages
91
Office Version
  1. 2016
Platform
  1. Windows
Hi,

How do I split phone and email address merge in a cell? Below are the examples and the needed results:
  • There are instances where there is no phone or no email or there is 2 or more emails.
  • The phone is always one time only if there is and the result should replace characters () and -
  • The email is always with "@" and there might be 2 or more emails, the result should have a separator ;
  • There are extra spaces to be eliminated with TRIM/CLEAN
Phone / EmailREMARKSPhone ResultEmail Result
(925) 474-1000 abcdefghij@yahoo.orgUsual sample
9254741000​
abcdefghij@yahoo.org
(925) 828-1000 a2z@google.comUsual sample
9258281000​
a2z@google.com
yesyes@outlook.orgEmail Onlyyesyes@outlook.org
(925) 828-1000 a2z@google.com abcdefghij@yahoo.org2 emails
9258281000​
a2z@google.com; abcdefghij@yahoo.org
(925) 800-1000 hithere@yahoo.comUsual sample
9258001000​
hithere@yahoo.com

Thank you in advance!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How about this?

Austin
ABCD
1Phone / EmailREMARKSPhone ResultEmail Result
2(925) 474-1000 abcdefghij@yahoo.orgUsual sample(925) 474-1000 abcdefghij@yahoo.org
3(925) 828-1000 a2z@google.comUsual sample(925) 828-1000 a2z@google.com
4yesyes@outlook.orgEmail Only yesyes@outlook.org
5(925) 828-1000 a2z@google.com abcdefghij@yahoo.org2 emails(925) 828-1000 a2z@google.com;abcdefghij@yahoo.org
6(925) 800-1000 hithere@yahoo.comUsual sample(925) 800-1000 hithere@yahoo.com
Sheet5
Cell Formulas
RangeFormula
C2:C6C2=IFERROR(LEFT(A2,SEARCH(" ",A2,SEARCH(" ",A2)+1)),"")
D2:D6D2=SUBSTITUTE(SUBSTITUTE(A2,C2,"")," ",";")
 
Upvote 0
@lrobbo314 Thanks for the quick response. The formula with Column C won't work as the " " has a different " " character from raw data. Also, there might be instances too the the phone number might not have "()". It won't be too consistent for phones. It can be 10 - 16 characters due to added spaces, parenthesis and hypen, but it will always need to pick the 10 digit phone number.
 
Upvote 0
Does this work?

Cell Formulas
RangeFormula
C2:C6C2=IFERROR(LEFT(A2,FIND(CHAR(160),A2)),"")
D2:D6D2=SUBSTITUTE(SUBSTITUTE(A2,C2,""),CHAR(160),";")
 
Upvote 0
@lrobbo314 I am not really sure how consistent the " " character will be once paste in excel from our intranet. For now the CHAR(160) works but would it possible to make it error proof? Say an example will be "9254741000 abcdefghij@yahoo.org" where the " " will really be space char. The formula above will no longer work. Also please, will need a formula in Column E to clean the phone numbers. Will only need the 10digits, no (), no spaces, no dash and other characters except for the numbers.
 
Upvote 0
Going to need to switch to VBA on this one.

austin
ABCD
1Phone / EmailREMARKSPhone ResultEmail Result
2(925) 474-1000 abcdefghij@yahoo.orgUsual sample(925) 474-1000abcdefghij@yahoo.org
3(925) 828-1000 a2z@google.comUsual sample(925) 828-1000a2z@google.com
4yesyes@outlook.orgEmail Only yesyes@outlook.org
5(925) 828-1000 a2z@google.com abcdefghij@yahoo.org2 emails(925) 828-1000a2z@google.comabcdefghij@yahoo.org
6(925) 800-1000 hithere@yahoo.comUsual sample(925) 800-1000 hithere@yahoo.com
79254741000 test.gmail.comAnother Sample9254741000test.gmail.com
Sheet7
Cell Formulas
RangeFormula
C2:C7C2=IFERROR(Phone(A2),"")
D2:D7D2=SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A2,C2,""))," ",";"),CHAR(160),"")


Here is the code for the VBA function 'Phone'...

VBA Code:
Function Phone(s As String)
With CreateObject("VBScript.RegExp")
    .Pattern = "\(?\d{3}\)?\s?\d{3}\-?\d{4}"
    Phone = .Execute(s)(0)
End With
End Function
 
Upvote 0
Missed the part that you wanted to clean the phone numbers as well.

Cell Formulas
RangeFormula
C2:C7C2=IFERROR(Phone(A2),"")
D2:D7D2=SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A2,C2,""))," ",";"),CHAR(160),"")


VBA Code:
Function Phone(s As String)
With CreateObject("VBScript.RegExp")
    .Pattern = "\(?\d{3}\)?\s?\d{3}\-?\d{4}"
    Phone = xClean(.Execute(s)(0))
End With
End Function

Function xClean(s As Variant)
Dim chrs As Variant:    chrs = Array("(", ")", "-", " ", Chr(160))

For i = LBound(chrs) To UBound(chrs)
    s = Replace(s, chrs(i), "")
Next i
xClean = s
End Function
 
Upvote 0
Solution
@lrobbo314 I have used the first Phone Function as the second with the combined xClean and Phone will overwrite what was in Column D so I just used an xClean function separately in Column E the substitute formula to work just fine. Then I change the formula in D2 to =SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(A2,C2,"")),CHAR(160),"",1),CHAR(160),"; "). Now it works perfectly fine. Thank you! 😁
 
Upvote 0
If you'd rather. I changed the code to run as a subroutine instead of using functions.

Just run 'PHONEX' and it should turn this...

Book1
ABCD
1Phone / EmailREMARKSPhone ResultEmail Result
2(925) 474-1000 abcdefghij@yahoo.orgUsual sample
3(925) 828-1000 a2z@google.comUsual sample
4yesyes@outlook.orgEmail Only
5(925) 828-1000 a2z@google.com abcdefghij@yahoo.org2 emails
6(925) 800-1000 hithere@yahoo.comUsual sample
79254741000 test.gmail.comAnother Sample
Sheet1


into this...

Book1
ABCD
1Phone / EmailREMARKSPhone ResultEmail Result
2(925) 474-1000 abcdefghij@yahoo.orgUsual sample9254741000abcdefghij@yahoo.org
3(925) 828-1000 a2z@google.comUsual sample9258281000a2z@google.com
4yesyes@outlook.orgEmail Onlyyesyes@outlook.org
5(925) 828-1000 a2z@google.com abcdefghij@yahoo.org2 emails9258281000a2z@google.com;abcdefghij@yahoo.org
6(925) 800-1000 hithere@yahoo.comUsual sample9258001000hithere@yahoo.com
79254741000 test.gmail.comAnother Sample9254741000test.gmail.com
Sheet1


VBA Code:
Function Phone(s As Variant)
On Error GoTo ERH

With CreateObject("VBScript.RegExp")
    .Pattern = "\(?\d{3}\)?\s?\d{3}\-?\d{4}"
    Phone = .Execute(s)(0)
End With
Exit Function
ERH:
If Err.Number = 5 Then
    Resume Next
Else
    MsgBox "Error#" & Err.Number & vbLf & Err.Description
End If
End Function

Function xClean(s As Variant)
Dim chrs As Variant:    chrs = Array("(", ")", "-", " ", Chr(160))

For i = LBound(chrs) To UBound(chrs)
    s = Replace(s, chrs(i), "")
Next i
xClean = s
End Function

Function CHL(s As Variant) As String
Dim cd As Integer:  cd = Asc(Left(s, 1))
Debug.Print cd
If cd = 32 Or cd = 160 Then
    CHL = Right(s, Len(s) - 1)
Else
    CHL = s
End If
End Function

Sub PHONEX()
Dim r As Range:         Set r = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim RES() As Variant:   ReDim RES(1 To UBound(AR), 1 To 2)
Dim p As String, e As String

For i = 1 To UBound(AR)
    p = Phone(AR(i, 1))
    e = Replace(AR(i, 1), p, "")
    RES(i, 1) = xClean(p)
    RES(i, 2) = Replace(Replace(CHL(e), Chr(160), ";"), " ", ";")
Next i

r.Offset(, 2).Resize(r.Rows.Count, 2).Value = RES
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,097
Members
449,096
Latest member
provoking

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top