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
[/TD]
[/TR]
</tbody>[/TABLE]
[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
[/TR]
</tbody>[/TABLE]
Last edited: