Check box to change the format of a text box

R

Rachel

I have a text box (txtphone) in a user form that a user will enter a phone
number into. The default format of this text box is to be "00-0000-0000". I
want them to be able to tick a check box (chkmobile) if the number is a
mobile number and the format of txtphone is to change to "0000-000-000". If
they then untick chkmobile the format returns to "00-0000-0000". Is this
possible?

Thanks :)
 
J

john

Rachel,

place both these procedures behind your form & see if it does what you want.
the Exit routine ensures format is applied after user has updated entry.

Hope helpful

Private Sub chkmobile_Click()

With Me.txtphone

.Text = Replace(Replace(.Text, " ", ""), "-", "")

If Me.chkmobile.Value = True Then

.Text = Format(.Text, "0000-000-000")

Else

.Text = Format(.Text, "00-0000-0000")

End If

End With

End Sub

Private Sub txtphone_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Call chkmobile_Click
End Sub
 
J

Jacob Skaria

Assuming you have the default format as mentioned try the below checkbox
click event..

Private Sub chkmobile_Click()
txtphone.Text = IIf(chkmobile.Value, "0000-000-000", "00-0000-0000")
End Sub

If this post helps click Yes
 
J

Jacob Skaria

If the user type in the phone number before checking mobile then you can use
the below which would change the format of the existing number..

Private Sub chkmobile_Click()
txtphone.Text = Format(Replace(txtphone.Text, "-", ""), _
IIf(chkmobile.Value, "0000-000-000", "00-0000-0000"))
End Sub

If this post helps click Yes
 
R

Rachel

Hi John,
Sorry for the late response - I actually couldn't find my question after
posting it!!
Your codes worked perfectly, thank you so much!!
 

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

Top