Format A TextBox For A Phone Number

  • Thread starter Thread starter Minitman
  • Start date Start date
M

Minitman

Greetings,

I am trying to format a TextBox on a UserForm to display a phone
number in xxx-xxx-xxxx format when I exit the TextBox.

Any help would be appreciated.

TIA

-Minitman
 
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Textbox1.Text = format(textbox1.text,"000-000-0000")
End Sub
 
Try this-Tested!!:

Private Sub TextBox1_Change()
If Len(TextBox1.Text) = 3 Then
TextBox1.Text = TextBox1.Text & Chr(45)
End If
If Len(TextBox1.Text) = 7 Then
TextBox1.Text = TextBox1.Text & Chr(45)
End If
TextBox1.MaxLength = 12
End Sub

Should you want to apply this to a number of TextBoxes, use the modified
version:

Private Sub FormatText()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
ctl.MaxLength = 12
If Len(ctl.Text) = 3 Then
ctl.Text = ctl.Text & Chr(45)
End If
If Len(ctl.Text) = 7 Then
ctl.Text = ctl.Text & Chr(45)
End If
End If
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top