View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.worksheet.functions
rexie3 rexie3 is offline
external usenet poster
 
Posts: 14
Default How do I create a macro to add zeros in front of a value?

Thank you Don for being so patient with me....

(You too Gord!)

I'm soooo close I can taste it. I'm trying to end up with 10 characters for
each cell.
For now I'm using your macro with the IF THEN statements and it's working
great.

But for later on, I sure would like to know how to combine your macro with
Gord's macro which asks the user for a range. I tried to type in the
underscore and Excel doesn't like it. What does "Type:=8" mean? I'm really
trying to understand how to write macros because this new job of mine has
MACROS! written all over it!


Gord's macro states:
Sub test33()
Set thisrng = Application.InputBox(prompt:= _
"Select the range of cells.", Type:=8)
For Each cell In thisrng
cell.Value = "00" & cell.Value
Next
End Sub


So far I have this macro written out and it doesn't want to work. Can a
macro like this even be done?

Sub Addzeros()

Set thisrng = Application.InputBox(prompt:="Select the range of cells.",
Type:=8)
For Each cell In thisrng
If Len(cell.Value ) = 3 Then cell.Value = "0000000" & cell.Value
If Len(cell.Value ) = 4 Then cell.Value = "000000" & cell.Value
If Len(cell.Value ) = 5 Then cell.Value = "00000" & cell.Value
If Len(cell.Value ) = 6 Then cell.Value = "0000" & cell.Value
If Len(cell.Value ) = 7 Then cell.Value = "000" & cell.Value
If Len(cell.Value ) = 8 Then cell.Value = "00" & cell.Value
If Len(cell.Value ) = 9 Then cell.Value = "0" & cell.Value
Next
End Sub

"Don Guillett" wrote:

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.