Insert a space into phone numbers

worksop

New Member
Joined
Jul 15, 2018
Messages
6
Hi, I am new to VBA and trying to get to grips with it.
I have a large list of phone numbers that are in different formats i.e. 01234 456 789, or 01234 456789 ect.
I have managed to clear all spaces out to leave continuous numbers, now I want to insert a space after 5th digit of the area code and 3rd digit of the number i.e. 01234 245 567.
Is there a way insert spaces in the current cell? with out Concatenate or copying to other cells.
Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
This macro will handle everything... it removes the spaces (if any) from the original data and then inserts the spaces where you want them. Note... you did not say where your phone numbers were located at, so I assumed Column A starting at Row 1, change the red highlighted part as needed...
Code:
Sub FixPhoneNumbers()
  With Range("[B][COLOR="#FF0000"]A1[/COLOR][/B]", Cells(Rows.Count, "[B][COLOR="#FF0000"]A[/COLOR][/B]").End(xlUp))
    .Replace " ", "", xlPart, , , , False, False
    .Value = Evaluate("TEXT(" & .Address & ",""0000 000 0000"")")
  End With
End Sub
 
Upvote 0
Hi Rick, I have tried it out but unfortunately all the numbers are converted to 0000, 000, 000.
Is there a way to can I use active cell or some other reference?.
Thanks in advance.
 
Upvote 0
Welcome to the MrExcel board!

If you have already stripped out the spaces, see how this one goes.
Code:
Sub FormatPhoneNumbers1()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    .Value = Evaluate(Replace("if(#="""","""",text(#,""00000 000 000""))", "#", .Address))
  End With
End Sub

If you want the code to do the 'space-stripping' as well as the formatting on your raw data, try
Code:
Sub FormatPhoneNumbers2()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    .Value = Evaluate(Replace("if(#="""","""",text(substitute(#,"" "",""""),""00000 000 000""))", "#", .Address))
  End With
End Sub
 
Last edited:
Upvote 0
Hi Peter_SSs.
the sub formatphonenumber2 work a treat please see below.
(I installed Mrexcell HTML Maker, followed the instructions the Mr excell tab was displayed but the diaspeared is that normal?.
Anyway THANKS for YOUR HELP>:cool:


After Befor
20831 111 144 0208 31111144
02072 325 636 0 20 7232 5636
01233 621 839 01233 621839
01622 726 262 01622 726262
07774 591 470 07774 591470
 
Upvote 0
Hi Peter_SSs.
the sub formatphonenumber2 work a treat please see below.
That's great, but I note that the first example contains 12 digits whereas I us under the impression that your numbers were all 11 digits. Is that 12-digit example correct and, if so, how should that number be formatted as we cannot do 5-3-3?


(I installed Mrexcell HTML Maker, followed the instructions the Mr excell tab was displayed but the diaspeared is that normal?.
I suspect that you may not have taken note of this from the 'Attachments' link page:
N.B. With any of the Add-Ins with recent versions of Windows, you may need to "unblock" the file. In Windows Explorer, find the file, right-click, Properties, General tab, tick the 'Unblock' box down near the bottom ..
 
Upvote 0
This macro will handle everything... it removes the spaces (if any) from the original data and then inserts the spaces where you want them. Note... you did not say where your phone numbers were located at, so I assumed Column A starting at Row 1, change the red highlighted part as needed...
Code:
Sub FixPhoneNumbers()
  With Range("[B][COLOR=#ff0000]A1[/COLOR][/B]", Cells(Rows.Count, "[B][COLOR=#ff0000]A[/COLOR][/B]").End(xlUp))
    .Replace " ", "", xlPart, , , , False, False
    .Value = Evaluate("TEXT(" & .Address & ",""0000 000 0000"")")
  End With
End Sub

Rick,
your code puts spaces correctly at the first number (A1) and then copies that number to the entire A column.
You can check, please. Thank you.
 
Upvote 0
Rick,
your code puts spaces correctly at the first number (A1) and then copies that number to the entire A column.
You can check, please. Thank you.
Yep, you are correct. Because TEXT is a text string function, you must induce array handling into it which I forgot to do.:banghead: Thanks for catching that. This revised code should work correctly...
Code:
Sub FixPhoneNumbers()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    .Replace " ", "", xlPart, , , , False, False
    .Value = Evaluate("[B][COLOR="#FF0000"]IF({1},[/COLOR][/B]TEXT(" & .Address & ",""0000 000 0000"")[B][COLOR="#FF0000"])[/COLOR][/B]")
  End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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