Forum

Notifications
Clear all

formatting cells

5 Posts
3 Users
0 Reactions
87 Views
(@alinhart)
Posts: 39
Trusted Member
Topic starter
 

Hello. I need help please. I have an excel spreadsheet we get from our customer.  Column A has 5 digit number, example 12345.  They have formatted the cell under special, adding in a leading zero and adding in a dash between, so finished number looks like, 012-345.

I have a macro that removed the formatting, adds in the leading zero.  What I need to complete is to add in the dash.  Also it needs to be save as text, not a number.  Any help would be greatly appreciated.

Sub LeadingZeros1()

Dim cel As Range, rg As Range

Application.ScreenUpdating = False

Set rg = Range(Range("A3"), Cells(Rows.Count, 1).End(xlUp))

rg.NumberFormat = "@"

For Each cel In rg.Cells
cel.Value = "0" & cel.Value

Next

End Sub

 

I have tried “0##-###”, but I don’t know what code to use….

Thanks so much

Amy

 
Posted : 19/02/2020 7:20 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Amy

Try changing this line from

cel.Value = "0" & cel.Value

to

cel.Value = Format(cel.Value, "000-000")

Hope this helps

Sunny

 
Posted : 19/02/2020 11:17 pm
(@purfleet)
Posts: 412
Reputable Member
 

You could just do this in a formula if it helps - i have used this so many times in my work.

=REPT("0",6-LEN(A1))&A1

Purfleet

 
Posted : 20/02/2020 12:25 am
(@sunnykow)
Posts: 1417
Noble Member
 

To include a dash, you can try TEXT(A3,"000-000")

 
Posted : 20/02/2020 1:32 am
(@alinhart)
Posts: 39
Trusted Member
Topic starter
 

SunnyKow

Thank you so much.  This works great!  

Amy

 
Posted : 20/02/2020 12:24 pm
Share: