Phone Number Data Validation

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
I need some help with some data validation. I have a form that populates with data from a worksheet. Part of this data is a phone number. I've tried the code below, but I can't seem to get it to work.

What I want:
If the value in txt_Phone is numeric and 10 digits, the value gets formatted as (###)###-####.
If the value is 13 characters long, the sub exits, as that would be the phone number that loaded into the form.
Any other condition would present the msg box.

What I'm getting:
I can type in a 10 digit number that converts, but I get the msg box when I click the command button that evaluates everything.
If I leave the value that loaded into the text box (formatted as (###)###-####), I still get the msg box.

Code:
If IsNumeric(Me.txt_Phone.Value) And Len(Me.txt_Phone.Value) = 10 Then    Me.txt_Phone = Format(Me.txt_Phone.Value, "(###)###-####")
If Len(Me.txt_Phone.Value) = 13 Then Exit Sub
Else
    MsgBox "Please enter a valid phone number for the Client, using numbers only."
    If response = vbOK Then Me.txt_Phone.SetFocus
    Exit Sub
End If

Can anyone offer some advice on this please?
 
The only dash would be after the 6th number, but it seems as though the last bit of code pointed me towards the solution. Thank you for your time!!!
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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