Thread: Text Formats
View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Text Formats

As others have pointed out, you can use event programming to correct the
entry (formatting would only change the display of the entry, not the actual
entry itself, so formatting would not be the appropriate way to do what you
asked). Here is my Change event suggested solution...

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Columns("C")) Is Nothing Or Target.Value = "" _
Or Target.Value Like "[a-zA-Z]#######-##" Then Exit Sub
If Target.Value Like "[a-zA-Z]#########" Then
Application.EnableEvents = False
Target.Value = Left(Target.Value, 8) & "-" & Right(Target.Value, 2)
Application.EnableEvents = True
Else
MsgBox "That entry is incorrect", vbCritical, "Bad Entry"
End If
End Sub

You can set the column to monitor in the first If statement (inside the
Columns property call)... just change it to the column letter you want to
apply this functionality to. Also note that the code will warn the user if
the entry does not meet the correct format (letter followed by 9 digits).

--
Rick (MVP - Excel)



"Brad E." wrote in message
...
My Understanding: When using VALUES, one can format a cell to display a
SSN
or ZIP+4 using Format Cells Custom. Of course, Excel has a couple built
in, including these two, but you could make any format you want as long as
the entry is a value.

My Problem: I have part numbers which start with an Alpha character
followed by 7 digits, then a dash (minus) and two more digits. Is there a
way to format my cell entry to include the dash when the user just enters
the
10-character part number?

Specific Example: If my part number if P1234567-89, users sometimes enter
P123456789. I would like to custom format the cell to automatically put a
dash in if it is not included. Not all my parts start with P.
--
TIA, Brad E.