Add Leading Zero's To Alphanumeric Field

tdio

New Member
Joined
Apr 12, 2011
Messages
5
Hello All,
I know how to pad out a cell with leading zero's if the data in the cell is numeric, but when it's alphanumeric, i'm hitting a brick wall.

Some examples of item numbers we have in column A are;
B-0035
B-12X16PLN
B-210

I need all entries in column A to be 14 characters long, with zeros being the padding ie;
00000000B-0035
0000B-12X16PLN
000000000B-210


Any ideas?

Thanks Tdio
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Assuming your first bit of data is in A7 this will make the cell 14 characters long and fill the beginning with leading 0s.

=REPT("0",14-LEN(A7))&A7
 
Upvote 0
You can also use VBA if you actually want to change the data in A. Highlight the cells that you need converted to 14 digits.

Just highlight what you want to convert to 14 digits and then run the macro. Be careful not to select the whole column as the macro will take forever to run.

Code:
Sub Digits()
For Each c In Selection
    If Len(c.Value) < 14 And Len(c.Value) <> "" Then
        c.Value = WorksheetFunction.Rept("0", 14 - Len(c.Value)) & c.Value
    End If
Next
End Sub
 
Upvote 0
You can also use VBA if you actually want to change the data in A. Highlight the cells that you need converted to 14 digits.

Just highlight what you want to convert to 14 digits and then run the macro. Be careful not to select the whole column as the macro will take forever to run.

Code:
Sub Digits()
For Each c In Selection
    If Len(c.Value) < 14 And Len(c.Value) <> "" Then
        c.Value = WorksheetFunction.Rept("0", 14 - Len(c.Value)) & c.Value
    End If
Next
End Sub


Worked like a charm. Thanks for all your help.
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,818
Members
452,946
Latest member
JoseDavid

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