Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 49948

Macro to include dashes into identification number

$
0
0
SOLVED!

Hey I am trying to include dashes into an identification number based on the number of characters in the cell. For example I want to turn A12010 to A1-20-10 or A120010 to A1-200-10.
The identification numbers will only ever be 6 or 7 characters long with the first and last two characters being separated by dashes. Here is my code

Code:

Sub ReplaceDash()
    Dim Cell As Range
    For Each Cell In Selection
        If Len(Cell) = 6 Then
            Cell.Value = Left(Cell,2) & "-" & Mid(Cell,3,2) & "-" Right(Cell,2)
        ElseIf Len(Cell) = 7 Then
            Cell.Value = Left(Cell,2) & "-" & Mid(Cell,3,3) & "-" Right(Cell,2)
        End If
    Next
End Sub

Sorry I am new to this. Is this not a correct approach?

Viewing all articles
Browse latest Browse all 49948

Trending Articles