soulpow3r
soulpow3r

Reputation: 55

Macro for adding zeroes to the front of numbers in specific Word doc table columns

Can anyone offer any VBA macro suggestions?

In a Word doc table, I need to apply the following rule to the 1st column only:

I need to add zeroes (0s) to the front of numbers to bring them up to a total of 8 characters long. All of the numbers end with a letter or two, which are to be included in the 8 character count.

For example:

2020A (5 characters) must read 0002020A (8 characters)
123456AB (8 characters) remains unchanged
765432X (7 characters) must read 0765432X (8 characters)

How do I apply this to every field in the FIRST COLUMN ONLY of a table on a Word doc?

Upvotes: 0

Views: 1635

Answers (1)

soulpow3r
soulpow3r

Reputation: 55

Answer provided by macropod on vbaexpress forum:

Dim RngCel As Range, oCel As Cell, oTbl As Table 
For Each oTbl In ActiveDocument.Tables 
    For Each oCel In oTbl.Columns(1).Cells 
        Set RngCel = oCel.Range 
        RngCel.End = RngCel.End - 1 
        While Len(RngCel.Text) < 8 
            RngCel.InsertBefore "0" 
        Wend 
    Next oCel 
Next oTbl

http://www.vbaexpress.com/forum/showthread.php?p=254288#post254288

Upvotes: 3

Related Questions