I am trying to make a userform text box moreorless like you often see for phone numbers. If you start typing, once you get the area code in, it enters a hyphen. You type the next three it enters another. However, I am entering numbers where the last digit always needs to be separated, and then the preceding two while the first string can be varying lengths.
I've tried combinations of the Left, Right, Mid, but the problem with this approach is that starts to include the hyphen in the count. I've tried to think about how to access / store it within a string but that fell flat as well. This seemed the most promising, but maybe not.
- Private Sub CASTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
- KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[0-9]")
- Select Case Len(CASTextBox)
- Case Is > 3: CASTextBox = Left(CASTextBox, Len(CASTextBox) - 4) & "-" & _
- Mid(CASTextBox, Len(CASTextBox) - 4, 2) & "-" & _
- Right(CASTextBox, 2)
- End Select
- End Sub
I've attached a workbook ( CAS Hyphens.xlsm ) just for a little playground and starting point apart from my greater machine.
Thank you for any and all assist. If you would like to send me off with a different approach, I have now taken a short course (8.5 hours of video) on VBA and shouldn't be quite so dense if you try to explain it to me.