Forms

Xvanwyk

New Member
Joined
Oct 31, 2017
Messages
1
Hi guys and gals, I want to create a spreadsheet containing cells with fixed characters after every 5 manually entered characters:
e.g. xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
So basically I want to type all 25 characters but have a "-" after every 5th character.

Can anyone possibly assist me in doing this?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Here is a VBA option as well. This will automatically insert the dashes without needing to use formulas. Note that if you type in a 25 digit number excel will convert it to scientific notation and drop several numbers, so make sure to format the cells that you are going to be entering the numbers into as text.

Right click on the sheet that you are entering the numbers into, click 'View Code' and paste the following code. Also, adjust the line of code with the note to match the range where your data will be.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A13")) Is Nothing Then 'Change to your range
    If Len(Target.Value) > 0 Then
        Application.EnableEvents = False
        Dim AR() As String
        Dim cnt As Long
        ReDim AR(1 To Len(Target) / 5)
        Dim res As String
        
        cnt = 1
        For i = 1 To Len(Target)
            AR(cnt) = AR(cnt) & Mid(Target.Value, i, 1)
            If i Mod 5 = 0 Then cnt = cnt + 1
        Next i
        
        res = Join(AR, "-")
        Target.Value = res
        Application.EnableEvents = True
    End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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