View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Don Guillett Don Guillett is offline
external usenet poster
 
Posts: 10,124
Default How do I create a macro to add zeros in front of a value?

You need to clarify what condition causes 3 and what causes 2. If you are
trying to get all to line up, use my original suggestion. Gord's macro
figured the number of rows for you but if you want to specify.

Sub addzerosinfront()
Range("c1:c4").NumberFormat = "@"
For Each c In Range("c1:c4")
If Len(c) = 5 Then c.Value = "0" & c
If Len(c) = 4 Then c.Value = "00" & c
Next
End Sub

--
Don Guillett
SalesAid Software

"rexie3" wrote in message
...
Thank you so much! Question: Would I be able to run this macro for a
number
of rows within my column? For example, I would want to run it for 20 rows
in
Column B. How do I do that?

I have to add 3 zeros in some cells and others 2 zeros, etc. How do I
tell
the macro to repeat itself for a number of rows down a column?

"Gord Dibben" wrote:

Sub test()
Set thisrng = Range(ActiveCell, Cells(Rows.Count, _
ActiveCell.Column).End(xlUp))
For Each cell In thisrng
cell.Value = "00" & cell.Value
Next
End Sub


Gord Dibben MS Excel MVP

On Mon, 18 Sep 2006 09:15:02 -0700, rexie3

wrote:

PLEASE SOMEONE HELP ME!!!

I have to add zeros to over 61,000 line items in excel. I need to know
how
to create a macro that will enable me to select a range within the same
column and have zeros added to the the numbers in the cell.

I would be eternally greatful.
Thanking you in advance.