allow phone numbers with spaces on a user form

tony.reynolds

Board Regular
Joined
Jul 8, 2010
Messages
97
Hello all,

I have a user form that have a phone and a fax field that i dont want letters entered.

i have in my code

If Not IsNumeric(Phone.Value) Then
MsgBox "Phone number not valid"
Exit Sub
End If

But dosnt allow number with spaces
ideally the number format should only allow XX XXXX XXXX format

can someone help me write my code ? :confused:
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi

Perhaps try something like this:

Code:
Dim strPhoneNo As String
Dim dblPhoneNo As Double
 
strPhoneNo = Me.TextBox1.Value  'or whatever
 
strPhoneNo = Replace(strPhoneNo," ","")
 
On Error Resume Next
dblPhoneNo = CDbl(strPhoneNo)
On Error Goto 0
 
If dblPhoneNo <> 0 Then
  'phone number OK!
Else
  'phone number not in correct format!
End if
 
Last edited:
Upvote 0
ideally the number format should only allow XX XXXX XXXX format
Possibly this?
Code:
With CreateObject("VBScript.RegExp")
    .Pattern = "\d{2} \d{4} \d{4}"
    If Not (Len(Phone.Value) = 12 And .Test(Phone.Value)) Then
        MsgBox "Phone number not valid"
    End If
End With
 
Upvote 0
Come to think of it you could just do it like:

Code:
strPhoneNo = Me.Textbox1.Value 'or whatever
 
If strPhoneNo Like "## #### ####" Then
   'phone number OK!
Else
  'phone number not OK :(
End if
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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