Insert character automatically in Userform Textbox

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I have a Textbox which is limited to 18 characters - what I need is for a forwardslash to be inserted automatically after the 1st five characters and then again after the 2nd six, so the result would look like this;

ABCDE/123456/FGHIJ

If the user puts the slashes in themselves then I'd need the automatic formatting to be stopped.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Do you require that the first batch be alphabetic, the next be numeric and the last group alphabetic?
 
Upvote 0
Mi Mike - the Textbox has a combination of letters and numerics and it doesn't matter which order they go in, but I would like to limit it to only these and slashes, so nothing else could be entered if that makes a difference?
 
Upvote 0
Perhaps this would work.
Code:
Private Sub TextBox1_Change()
    Const Delimiter As String = "/"
    Dim RawString As String
    
    RawString = Replace(TextBox1.Text, Delimiter, vbNullString)
    RawString = WorksheetFunction.Replace(RawString, 6, 0, Delimiter)
    RawString = WorksheetFunction.Replace(RawString, 12, 0, Delimiter)
    RawString = Replace(RawString, Delimiter & Delimiter, Delimiter)
    
    If Len(RawString) = 6 Or Len(RawString) = 12 Then
        Rem do nothing
    Else
        If Right(RawString, 1) = Delimiter Then
        RawString = Left(RawString, Len(RawString) - 1)
        End If
    End If
   
    TextBox1.Text = RawString
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top