VBA Macro change column contents

J

James C

I have a column in a worksheet which has a list of 3 digit numbers. I want to
change the 3 digits to become 4 digits, the new fourth digit has to be a
leading zero, i.e 355 to become 0355. I prefer the format of the digits to be
a number but a text format would not be a problem.

The length of the digits in the column are not static. i.e one month there
may be 200 rows of data the next might be 300 and I would want the macro to
cater for this variation.

Thanks
 
M

Mike H

James,

A macro simply isn't necessary, select the column and apply a custom format of
0000
that's four zeroes
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
G

Gary''s Student

Click on any cell in the column and run:

Sub MakeLeadingZero()
Dim ic As Long, n As Long, i As Long
Dim z As String
ic = ActiveCell.Column
n = Cells(Rows.Count, ic).End(xlUp).Row
z = "0"
For i = 1 To n
With Cells(i, ic)
v = .Value
.NumberFormat = "@"
.Value = z & v
End With
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top