Converting a column of data with varying digits

McDan

New Member
Joined
Jun 21, 2010
Messages
37
I have a column of numbers that constitute a code. The code is used instead of using a company's name. However, to run this code into a program I use for work, I need to make sure the code is in a certain format - it must have AA in front of it.

There is only one problem. The code must be 8 characters at the end. If the original code is 123456 we simply stick AA in the beginning - (AA123456). However, if the code is shorter we must input the correct amount of zeros in to compensate - so if it were only 1234 we would need to type AA001234.

What formula can I use to automatically convert this huge column of code into the correct AA format every time?

Thank You!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Like this...?

Code:
Sub test()
Dim LR As Long
Dim cell As Range

LR = Range("A" & Rows.Count).End(xlUp).Row
For Each cell In Range("A1:A" & LR)
    cell.NumberFormat = "AA000000"
Next cell
End Sub
 
Upvote 0
This might be better...

Code:
Sub test()
Dim i As Long
Dim j As Integer
Dim temp As String

For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
    If Len(Range("A" & i)) <= 6 Then
        temp = "AA"
        For j = 1 To 6 - Len(Range("A" & i))
            temp = temp & "0"
        Next j
        Range("A" & i) = temp & Range("A" & i)
    End If
Next i
End Sub
 
Upvote 0
Those are both really good suggestions but I was just shown another I thought I'd share - = ("AA" & Rept(0,6-len(A1))&A1)
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,877
Members
452,949
Latest member
Dupuhini

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