VBA 'Function' to clean up UK format numbers

Read_This

New Member
Joined
Aug 13, 2014
Messages
37
Guys, I wonder if you can help?

I using a VBA Funtion to clean up UK phone numbers to the required '01234 567890' format.
I'm currently using three different steps to get the result - I'm looking for all three steps to be combined into one VBA function script - ideally added to the existing function below.

The current VBA function and first step is;
============================
Function CleanUpNumber(StrNumber)
Dim StrNoSpaces, StrNoPluses, StrNoBracketsA, StrNoBracketsB, StrFinalNumber
StrNoSpaces = Replace(StrNumber, " ", "")
StrNoPluses = Replace(StrNoSpaces, "+", "0")
StrNoBracketsA = Replace(StrNoPluses, "(", "")
StrNoBracketsB = Replace(StrNoBracketsA, ")", "")
If Left(StrNoBracketsB, 3) = "044" Then
StrFinalNumber = "0" & Right(StrNoBracketsB, Len(StrNoBracketsB) - 3)
Else
StrFinalNumber = StrNoBracketsB
End If
CleanUpNumber = StrFinalNumber
End Function
============================

I then use;

=REPT(0,11-LEN(A1))&A1

as a second step to get a leading zero [if required], and then;

=REPLACE(A1,6,0," ")

as a third step to add a space character for the correct '01234 567890' format
[ the raw number above being in Cell A1 ]

Below is a sample of the raw data.

+44 8802192025
08802 798559
08802798559
8815698257

Any help/pointer would be appreciated.
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
See if this criteria fits; remove the spaces and take the right 10 digits and then format it with a space between the 4th and 5th digits and a zero at the front.

Code:
Sub formatPhone()For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
Cells(i, 1) = Format("0" & Right(Replace(Range("A1"), " ", ""), 10), "00000 000000")
Next
End Sub

You can do it with a formula and custom cell formatting (same format as above) if that works for you.
Code:
=0&RIGHT(SUBSTITUTE(A1," ",""),10)
 
Upvote 0
You can do it with a formula and custom cell formatting (same format as above) if that works for you.
Code:
=0&RIGHT(SUBSTITUTE(A1," ",""),10)
As long as you are going to use a formula, why not let it do everything instead of parceling some of it out cell formatting?

=TEXT(RIGHT(SUBSTITUTE(A1," ",""),10),"00000 00000")
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,794
Members
449,048
Latest member
greyangel23

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