Cleaning up contact information

Woodsa

New Member
Joined
Feb 26, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a column of contact information where it should just be one phone number, but it has been populated with multiple phone numbers, emails and various characters.

I have tried removing all characters and only taking the first 11 numerical values, but in some instances the column may have contained two phone numbers with the first number having the incorrect number of digits meaning I have taken the numbers from number 1 & number 2.

I am trying to split the numbers and emails exactly how they were entered (even if incorrect), that way I can perform validation on the numbers and clear any that fail validation. I'd also like to remove and +44 and replace with 0 etc..

Please see mock data below to show what I am trying to do.
Original DataDesired Result 1 (tel 1)Desired Result 2 (tel 2)Desired Result 3 (email 1)Desired Result 4 (email 2)
Phone: 02356 898/Mob: 078564985560235689807856498556
02365898756 / Email example@email.com02365898756 example@email.com
other@email.com 077854698563077854698563other@email.com
023659987585 mremail@email.com/mrsemail@email.co.uk023659987585mremail@email.commrsemail@email.co.co.uk
+44(0)136598567801365985678

Can anyone help with a way to achieve this?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
This sort of task can be quite tricky, particularly checking for valid email addresses. However, I think this should get you a reasonable way along the track. It will extract at most 2 of each type.

VBA Code:
Sub Phone_Email()
  Dim RX As Object, M As Object
  Dim a As Variant, b As Variant
  Dim i As Long, c As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 4)
  
  'Phone
  RX.Pattern = "(\(\d+\))?\d[\d ]{6,}"
  For i = 1 To UBound(a)
    c = 0
    For Each M In RX.Execute(a(i, 1))
      c = c + 1
      b(i, c) = Replace(Replace(Replace(M, " ", ""), "(", ""), ")", "")
      If c = 2 Then Exit For
    Next M
  Next i
  
  'Email
  RX.Pattern = "[A-Za-z0-9._\-]+@[A-Za-z0-9._\-]+"
  For i = 1 To UBound(a)
    c = 0
    For Each M In RX.Execute(a(i, 1))
      c = c + 1
      b(i, c + 2) = M
      If c = 2 Then Exit For
    Next M
  Next i
  
  'Results
  With Range("B2").Resize(UBound(b, 1), UBound(b, 2))
    .NumberFormat = "@"
    .Value = b
  End With
End Sub

My sample data and results:

Woodsa.xlsm
ABCDE
1Original DataDesired Result 1 (tel 1)Desired Result 2 (tel 2)Desired Result 3 (email 1)Desired Result 4 (email 2)
2Phone: 02356 898/Mob: 078564985560235689807856498556
302365898756 / Email example@email.com02365898756example@email.com
4other@email.com 077854698563077854698563other@email.com
5023659987585 mremail@email.com/mrsemail@email.co.uk023659987585mremail@email.commrsemail@email.co.uk
6+44(0)136598567801365985678
722222222 xvxzv 65656565dgfdg9898989865 abc@abc.com/abc1@abc.com/abc2@abc.com2222222265656565abc@abc.comabc1@abc.com
Sheet1
 
Upvote 1
Solution
This sort of task can be quite tricky, particularly checking for valid email addresses. However, I think this should get you a reasonable way along the track. It will extract at most 2 of each type.

VBA Code:
Sub Phone_Email()
  Dim RX As Object, M As Object
  Dim a As Variant, b As Variant
  Dim i As Long, c As Long
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 4)
 
  'Phone
  RX.Pattern = "(\(\d+\))?\d[\d ]{6,}"
  For i = 1 To UBound(a)
    c = 0
    For Each M In RX.Execute(a(i, 1))
      c = c + 1
      b(i, c) = Replace(Replace(Replace(M, " ", ""), "(", ""), ")", "")
      If c = 2 Then Exit For
    Next M
  Next i
 
  'Email
  RX.Pattern = "[A-Za-z0-9._\-]+@[A-Za-z0-9._\-]+"
  For i = 1 To UBound(a)
    c = 0
    For Each M In RX.Execute(a(i, 1))
      c = c + 1
      b(i, c + 2) = M
      If c = 2 Then Exit For
    Next M
  Next i
 
  'Results
  With Range("B2").Resize(UBound(b, 1), UBound(b, 2))
    .NumberFormat = "@"
    .Value = b
  End With
End Sub

My sample data and results:

Woodsa.xlsm
ABCDE
1Original DataDesired Result 1 (tel 1)Desired Result 2 (tel 2)Desired Result 3 (email 1)Desired Result 4 (email 2)
2Phone: 02356 898/Mob: 078564985560235689807856498556
302365898756 / Email example@email.com02365898756example@email.com
4other@email.com 077854698563077854698563other@email.com
5023659987585 mremail@email.com/mrsemail@email.co.uk023659987585mremail@email.commrsemail@email.co.uk
6+44(0)136598567801365985678
722222222 xvxzv 65656565dgfdg9898989865 abc@abc.com/abc1@abc.com/abc2@abc.com2222222265656565abc@abc.comabc1@abc.com
Sheet1
This is great, thank you!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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