Insert Dashes In Alpha/Numeric Sequence

data808

Active Member
Joined
Dec 3, 2010
Messages
358
Office Version
  1. 2019
Platform
  1. Windows
I wanted to format the cell to do this but looked online and it looks like this is not possible because of the alpha characters in the string with numbers.

In column C I will be entering a specific number that has letters and numbers. Here is the format:

AZ-1234-1-123456-1234

If there is a way to do it by just formatting the cell then please let me know. If not, what is the next best way to do it? VBA or Formula? Not sure how to do it so if you could send suggestions then that would be great.

Thank you.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
=LEFT(A1,2)&TEXT(RIGHT(A1,15),"-0000-0-000000-0000")
Sorry this did not work for me.

I am trying to type into column C cells except for rows 1 and 2 because those rows have my title headers. So basically I will type into cell C3, C4, C5, etc... and keep going down forever in that column. The values I will be typing into column C will be like this:

AZ123411234561234

but I want it to look like this when I tab over:

AZ-1234-1-123456-1234

Does that make sense?
 
Upvote 0
You do need to change the cell reference to where ever your data is. How is this not what you want?

Book2
CD
3AZ123411234561234AZ-1234-1-123456-1234
Sheet1
Cell Formulas
RangeFormula
D3D3=LEFT(C3,2)&TEXT(RIGHT(C3,15),"-0000-0-000000-0000")
 
Upvote 0
If you want it to happen in place, you'll need VBA.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, d As Range
Set d = Intersect(Target, Range("C3:C" & Range("C" & Rows.Count).End(xlUp).Row))
If d Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each c In d
    If Len(c) = 17 Then c = Left(c, 2) & Format(Right(c, 15), "-0000-0-000000-0000")
Next
Application.EnableEvents = True
End Sub
 
Upvote 1
Solution
If you want it to happen in place, you'll need VBA.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, d As Range
Set d = Intersect(Target, Range("C3:C" & Range("C" & Rows.Count).End(xlUp).Row))
If d Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each c In d
    If Len(c) = 17 Then c = Left(c, 2) & Format(Right(c, 15), "-0000-0-000000-0000")
Next
Application.EnableEvents = True
End Sub
Yes thank you. This is what I was looking for. I haven't tested it yet but I will soon and come back to give a status update. Thank you so much for the help.
 
Upvote 0
If you want it to happen in place, you'll need VBA.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, d As Range
Set d = Intersect(Target, Range("C3:C" & Range("C" & Rows.Count).End(xlUp).Row))
If d Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each c In d
    If Len(c) = 17 Then c = Left(c, 2) & Format(Right(c, 15), "-0000-0-000000-0000")
Next
Application.EnableEvents = True
End Sub
Working wonderfully. Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,206
Members
453,022
Latest member
RobertV1609

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