adding zeros in front of numbers

ammdumas

Active Member
Joined
Mar 14, 2002
Messages
469
Hey All,

Have a macros here that adds a new row and populates the first column with a code...

ActiveSheet.Unprotect
Rows(Range("A65536").End(xlUp).row).Copy Range("A65536").End(xlUp).Offset(1, 0)
Range("a65536").End(xlUp).Value = Range("F2").Value & "-" & Range("F3").Value & "-DS-" & Range("a65536").End(xlUp).row - 9
Range("e65536").End(xlUp).Value = ""
myrow = Range("A65536").End(xlUp).row - 1
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

The code produced looks something like '143232-133-DS-1' , where the '143232' and '133' come from cells F2 and F3. The incremental number comes from counting the rows in the sheet and where the data starts. Thing is, the first 99 codes need to have 3 digits at the end. So instead of '*-DS-1' it should be '*-DS-001' or '*-DS-026', etc.

How do I modify the code above to do that?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Range("a65536").End(xlUp).Value = Range("F2").Value & "-" & Range("F3").Value & "-DS-" & Range("a65536").End(xlUp).row - 9

becomes

Range("a65536").End(xlUp).Value = Range("F2").Value & "-" & Range("F3").Value & "-DS-" & format(Range("a65536").End(xlUp).row - 9,"000")

I won't get into how ugly this is as far as expensively doing the same thing (end(xlup)) six times.

{Edit}Removed erroneous parenthesis from formula.{/edit}
 
Upvote 0
ammdumas said:
Hey All,

Have a macros here that adds a new row and populates the first column with a code...

ActiveSheet.Unprotect
Rows(Range("A65536").End(xlUp).row).Copy Range("A65536").End(xlUp).Offset(1, 0)
Range("a65536").End(xlUp).Value = Range("F2").Value & "-" & Range("F3").Value & "-DS-" & Range("a65536").End(xlUp).row - 9
Range("e65536").End(xlUp).Value = ""
myrow = Range("A65536").End(xlUp).row - 1
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

The code produced looks something like '143232-133-DS-1' , where the '143232' and '133' come from cells F2 and F3. The incremental number comes from counting the rows in the sheet and where the data starts. Thing is, the first 99 codes need to have 3 digits at the end. So instead of '*-DS-1' it should be '*-DS-001' or '*-DS-026', etc.

How do I modify the code above to do that?

If You were entering a number in a cell you would need to setup a Custom number format for the maximum number of 0's you will need as the number itself grows the actual number will replace the leading zeros in the code. The format is 00#

In your case since you are concantenating a string from other cells you would have to build into your formula that if you have 1 digit you add "00" in the cell if two digits you add "0" in the cell.
 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,635
Members
449,043
Latest member
farhansadik

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