converting value to phone number format

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
Does anyone know how to use vba to format a phone number to the correct format? For some reason when a user enters a phone number with this format: (###)###-#### my script will not change the value to the correct format of: (###) ###-####. The only format difference is the space between the area code and the start of the phone number. The code below tests as true and executes the 2nd line of code but its almost as if the format function does not recognize the space. Do I need to use a special character or something to represent a space when formatting? Or am i doing this completely the wrong way?

not working:
Code:
If Len(me.txtPhone.Value) = 13 And me.txtPhone.Text Like "(###)###-####" Then
me.txtPhone.Value = Format(me.txtPhone.Text, "(###) ###-####")
End If

working:
Code:
If Len(me.txtPhone.Value) = 10 And me.txtPhone.Text Like "##########" Then
me.txtPhone.Value = Format(me.txtPhone.Text, "(###) ###-####")
End If
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I believe that the value is being viewed as text and therefore requires string manipulation. Try:

Code:
    If Len(Me.txtPhone.Value) = 13 And Me.txtPhone.Text Like "(###)###-####" Then
        Me.txtPhone.Value = Left(Me.txtPhone.Value, 5) & " " & Right(Me.txtPhone.Value, 8)
    End If
 
Upvote 0

Forum statistics

Threads
1,214,378
Messages
6,119,188
Members
448,873
Latest member
jacksonashleigh99

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