View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Format a textbox

One caution to the OP... your solution doesn't allow for normal editing.
First off, the arrow keys erase numbers from the end. Second, try this...
type 4 numbers, click into the number part inside the parentheses and start
typing more numbers... the format is not preserved.

--
Rick (MVP - Excel)


"Per Jessen" wrote in message
...
Hi

Sure it can be done.
In addition to this I would set the MaxLength (Properties) of the TextBox
to 13 to limit the number of digits after the hypen to 4.

Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If IsNumeric(Chr(KeyCode)) Then
If Len(Me.TextBox1) = 1 Then Me.TextBox1 = "(" & Me.TextBox1
If Len(Me.TextBox1) = 4 Then Me.TextBox1 = Me.TextBox1 & ")"
If Len(Me.TextBox1) = 8 Then Me.TextBox1 = Me.TextBox1 & "-"
Else
If Len(Me.TextBox1) < 0 Then
Me.TextBox1 = Left(Me.TextBox1, Len(Me.TextBox1) - 1)
End If
End If
End Sub

Regards,
Per

"JacyErdelt" skrev i meddelelsen
...
Could anyone tell me how I might be able to format a textbox to auto
format
as a person is entering data. For instance when a person is entering the
phone number (555)555-5555, the 1st 3 digits wills automatically be place
within the parenthesis and the hypen will be placed when the next 3
digits
have been entered. I know i can format the box after it has LostFocus,
but
all the time we use advanced stand-alone programs that can be setup to do
it
as the user is entering info and I thought maybe VBE could do it too. If
you
have any suggestions, let me know. Thank you.