Extract data from a text row

donlincolnmre2

Board Regular
Joined
Dec 23, 2006
Messages
142
Hello

I have a sheet that contains data in column B with text and phone numbers.

I’m looking to extract the phone numbers from column B and place it, in a BLANK ROW UNDER in column A and then delete that number from column B and then copy the row which had the number right next to the phone number where the number was placed, AS SHOWN IN THE FOLLOWING Google sheet link.


Any help will be greatly appreciated J

Thanks.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
AS SHOWN IN THE FOLLOWING Google sheet link.
Is this a Google Sheets question or Excel?

If Excel on Windows OS, try this macro with a copy of your workbook.

VBA Code:
Sub ExtractPhone()
  Dim RX As Object
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = " \d{3}\-\d{3}\-\d{4}"
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value2
  ReDim b(1 To UBound(a) * 2, 1 To 2)
  For i = 1 To UBound(a)
    k = k + 1
    b(k, 1) = a(i, 1)
    If RX.Test(a(i, 2)) Then
      b(k, 2) = RX.Replace(a(i, 2), " ")
      k = k + 1
      b(k, 1) = Mid(RX.Execute(a(i, 2))(0), 2)
      b(k, 2) = b(k - 1, 2)
    Else
      b(k, 2) = a(i, 2)
    End If
  Next i
  Range("D2:E2").Resize(k).Value = b
End Sub

If it is a Google Sheets question then post back to say so and then I can move the thread to the correct forum.
 
Upvote 0
This was the Excel question and i run the macro that you gave me, it worked great.!!!

The only thing i noticed that some times the number format changed and those numbers of course weren't extract, here is the format.

Assessor P.O. Box 1255, Weaverville, CA 96093 Phone Fax (530)623-8398 Email assessor@trinitycounty.org.
2101 E University Dr. Auburn, AL 36830-3334. ... Bakersfield, CA 93312-3209. (661) 589-4400. Bill's Drywall.

In the first one the ) are collapsed right next to the number and in 2nd format the area code as ( ) around them.

Is there is any way to modify the macro to check for those two formats and extracts those numbers also.

Thanks.

:)
 
Upvote 0
I was able to find a work around it, i searched for ( and replaced with nothing and then searched for ) and space and replaced with - and then the macro picked up that format and was able to extract the other 2 numbers.

Again thanks for all your help. :):)
 
Upvote 0
Glad you got a solution.

Without those Find/Replace processes, you could have used the blue modified pattern line below

However, this would extract the numbers just as they were in the original data. Perhaps you prefer to have all the numbers in a standard format and if you wanted the code to do that for you, you could make the second blue line change too.

Rich (BB code):
Sub ExtractPhone_v2()
  Dim RX As Object
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = " \(?\d{3}[\) \-]{1,2}\d{3}\-\d{4}"
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value2
  ReDim b(1 To UBound(a) * 2, 1 To 2)
  For i = 1 To UBound(a)
    k = k + 1
    b(k, 1) = a(i, 1)
    If RX.Test(a(i, 2)) Then
      b(k, 2) = RX.Replace(a(i, 2), " ")
      k = k + 1
      b(k, 1) = Replace(Application.Trim(Replace(Replace(RX.Execute(a(i, 2))(0), "(", " "), ")", " ")), " ", "-")
      b(k, 2) = b(k - 1, 2)
    Else
      b(k, 2) = a(i, 2)
    End If
  Next i
  Range("D2:E2").Resize(k).Value = b
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,755
Members
449,094
Latest member
dsharae57

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