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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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