[Macro help request] formatting the integers.

misiek

New Member
Joined
Jan 26, 2005
Messages
14
Hi all,

My first post here, I am a bit nervous.

First off, I consider myself as belonging to the pool of 99% Excel users who use 1% of its power. :biggrin:

Came across this forum the other day and it looked like the right place to ask questions.

Let me begin with the data structure:

ColumnA | ColumnB
AAA | 12345678
BBB | 90123456

I am trying to format the integer numbers in ColumnB in the following way:
If ColumnA reads AAA, add TRAILING zeros to the number in ColumnB of the same row until there are 14 digits there.
If ColumnA reads BBB, add LEADING zeros to the number in ColumnB until there are 10 digits there.

Could you guys/gals give me some examples on how this could be done?
Since there are potentially hundreds of rows that need to be formatted this way, doing this manually is a nightmare.

Thanks in advance

:biggrin:
 
jindon said:
Hi

Hummm, now I understand what you are after.
You didn't need col.A letter to lead.
Code:
Sub a() 
Dim r As Range 
For Each r In Range("a1", Range("a65536").End(xlUp)) 
    If Not IsEmpty(r) Then 
        If r.Value = "AAA" Then 
            r.Offset(, 2) = r.Offset(, 1) & _ 
                WorksheetFunction.Rept(0, 14 - Len(r.Offset(, 1))) 
        Else 
            r.Offset(, 2) = Format(r.Offset(, 1), "0000000000") 
        End If 
    End If 
Next 
End Sub
should work

rgds,
jindon

Almost there, this is the output:

AAA,12,12000000000000
BBB,34,34 (desired format is 0000000034)
AAA,56,56000000000000
BBB,78,78 (desired format is 0000000078)
AAA,90,90000000000000

Thanks :)

Sorry for being a pain-in-the-neck.
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,216,167
Messages
6,129,262
Members
449,497
Latest member
The Wamp

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