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:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
rather than changing it, would it be worth viewing as three digit

Thanks for your reply -yes it need to be 3 digits....I think I got it fixed by changing 0 to 00 but its still adding 00 down to line 246 -if I can only set the range fixed on E1-E20 -thank you
 
Upvote 0
if you selected the column, then custom format as 000, then you would see what you want
 
Upvote 0
yes but its still adding 000 246 lines down in column E -I dont want that to do that -only need that fixed if you can help please mole999

If you only want it to run from E1 to E20 why are you using Application.UsedRange in

Code:
Set selCol = Application.Intersect(ActiveSheet.UsedRange, _
                    ActiveSheet.Columns(ActiveCell.Column))

Do you really want the range fixed to E1 to E20 or do you actually want it to the last cell with a value in column E?
 
Upvote 0
If you only want it to run from E1 to E20 why are you using Application.UsedRange in

Code:
Set selCol = Application.Intersect(ActiveSheet.UsedRange, _
                    ActiveSheet.Columns(ActiveCell.Column))

Do you really want the range fixed to E1 to E20 or do you actually want it to the last cell with a value in column E?


Mark it must be only be the last data -that would be great. I have not written it I got the code from net trying to make it work
 
Upvote 0
What happens with...

Code:
Sub LeadZ()
Dim Rng As Range
Dim rVal As String
Dim selCol As Range

Set selCol = Sheets("Test").Range("E1:E" & Sheets("Test").Columns(5).Find("*", , xlValues, , xlRows, xlPrevious).Row)
    
  selCol.NumberFormat = "@"
  
  For Each Rng In selCol
  
    rVal = Rng.Value
    If Len(Rng.Value) < 3 Then
    Rng.Value = "00" & rVal
    End If
    
   Next

MsgBox "Process is complete."
End Sub
 
Upvote 0
What happens with...

Code:
Sub LeadZ()
Dim Rng As Range
Dim rVal As String
Dim selCol As Range

Set selCol = Sheets("Test").Range("E1:E" & Sheets("Test").Columns(5).Find("*", , xlValues, , xlRows, xlPrevious).Row)
    
  selCol.NumberFormat = "@"
  
  For Each Rng In selCol
  
    rVal = Rng.Value
    If Len(Rng.Value) < 3 Then
    Rng.Value = "00" & rVal
    End If
    
   Next

MsgBox "Process is complete."
End Sub


Marks thank you very much this solves it 100% -thank you great code!
 
Upvote 0
What happens with...

Code:
Sub LeadZ()
Dim Rng As Range
Dim rVal As String
Dim selCol As Range

Set selCol = Sheets("Test").Range("E1:E" & Sheets("Test").Columns(5).Find("*", , xlValues, , xlRows, xlPrevious).Row)
    
  selCol.NumberFormat = "@"
  
  For Each Rng In selCol
  
    rVal = Rng.Value
    If Len(Rng.Value) < 3 Then
    Rng.Value = "00" & rVal
    End If
    
   Next

MsgBox "Process is complete."
End Sub

Here is another way to write your LeadZ macro...
Code:
[table="width: 500"]
[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]
[/table]
 
Upvote 0

Forum statistics

Threads
1,224,943
Messages
6,181,918
Members
453,071
Latest member
Gizmo2024

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