Leading zeros

Excelnewbie001

Board Regular
Joined
Jan 25, 2017
Messages
79
I have the following code to add a leading zero to my data in column E1-E20 the problem is when I run this macro which is set to 3 digits it runs down 210 lines adding a 0 -it musnt only the data in column E -in this example the 4 should be 004 and the 40 should be a 400 it must leave all the data thats already in 3 numbers so it should only adress the leading zero -this code fix the leading zero but it doesnt fix the 40 adding a zero at the end -any help much appreciated as it needs tweaking
[TABLE="width: 115"]
<tbody>[TR]
[TD]Sample data in

Column E
4[/TD]
[/TR]
[TR]
[TD]22[/TD]
[/TR]
[TR]
[TD]40[/TD]
[/TR]
[TR]
[TD]13[/TD]
[/TR]
[TR]
[TD]301[/TD]
[/TR]
[TR]
[TD]310[/TD]
[/TR]
[TR]
[TD]211[/TD]
[/TR]
[TR]
[TD]220[/TD]
[/TR]
[TR]
[TD]130[/TD]
[/TR]
[TR]
[TD]121

This is the macro code

Code:
Sub LeadZ()
Dim Rng As Range
Dim rVal As String
Dim selCol As Range
Sheets("Test").Select
Range("E:E").Select


Set selCol = Application.Intersect(ActiveSheet.UsedRange, _
                    ActiveSheet.Columns(ActiveCell.Column))
    
  For Each Rng In selCol
    Rng.Select
    Selection.NumberFormat = "@"
    rVal = Rng.Value
    If Len(Rng.Value) < 3 Then
    Rng.Value = "0" & rVal
    End If
    
   Next
 
MsgBox "Process is complete."
End Sub
[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Addr = "E1:E" & Cells(Rows.Count, "E").End(xlUp).Row

Hi Rick, I deliberately chose the Find method over the Rows.Count as I felt it safer as not having enough info on the usedrange issue.
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Here is another way to write your LeadZ macro...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub LeadZ()
  Dim Addr As String
  Addr = "E1:E" & Cells(Rows.Count, "E").End(xlUp).Row
  Range(Addr).NumberFormat = "@"
  Range(Addr) = Evaluate(Replace("IF(LEN(@)<3,""00""&@,@)", "@", Addr))
End Sub[/TD]
[/TR]
</tbody>[/TABLE]


Fantastic thank you Rick this works too !
 
Upvote 0

Forum statistics

Threads
1,223,959
Messages
6,175,645
Members
452,663
Latest member
MEMEH

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