from 111 to 1-1-1

Jos1972

New Member
Joined
Nov 3, 2008
Messages
33
hello,
I am looking for a way to split numbers to text with dashes between the numbers (i.e. 111 should become 1-1-1). I used concatenate combined with Len to make the split, however sometimes the cell contains a 3 digit number, sometimes 4 digit, up to 7 digits.
Does anyone have experience with this problem? Is there one formula that I could use?, or a macro?


Thank
Jos
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
UDF.
Set reference: Tools -> References -> Microsoft VBScript Regular Expressions 5.5
Code:
Function SetDashes(Str As String)

    ' Set reference: Tools -> References -> Microsoft VBScript Regular Expressions 5.5
    
    Dim re As New RegExp
    Dim s As String
    
    With re
        .Global = True
        .Pattern = "(\d)"
    End With
    
    s = re.Replace(Str, "$1-")
    
    SetDashes = Left(s, Len(s) - 1)

End Function
 
Upvote 0
You could 4 conditional formatting rules.

Rule 1:
Code:
=IF(LEN(A1)=4,1,0)
Custom format would be #-#-#-#
Rule 2:
Code:
=IF(LEN(A1)=5,1,0)
Custom format would be #-#-#-#-#
Rule 3:
Code:
=IF(LEN(A1)=6,1,0)
Custom format would be #-#-#-#-#-#
Rule 4:
Code:
=IF(LEN(A1)=7,1,0)
Custom format would be #-#-#-#-#-#-#

/Comfy
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,488
Members
452,917
Latest member
MrsMSalt

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