Add a '0' to a number (1 Viewer)

noboffinme

Registered User.
Local time
Today, 14:55
Joined
Nov 28, 2007
Messages
288
Hi

I'm trying to add a '0' to a phone number field but cannot get my code to work & was wondering why it doesn't.

Here's the code;

------------------------------------------------------------------

Sub add_zero()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim m As Variant
Dim tmpstring As String

Sheets("DATA").Select

'i is a variable for the 'DATA' worksheets Row number, steps up one by one until it reaches Row 2
For i = Sheets("DATA").Range("H100000").End(xlUp).Row To 2 Step -1

j = 2
k = 8

tmpstring = Cells(i, k).Value

Cells(i, k).Select

'add a leading "0" if there was none
If Mid(tmpstring, 1, 1) <> "0" Then

tmpstring = "0" & tmpstring

tmpstring = Cells(i, k) '.Value

End If

Next i

End Sub
------------------------------------------------------------

The value of 'tmpstring' shows in the locals window as adding the zero the the front of the field value, but it doesn't overwrite the cell with this change.

Can anyone advise why?

Thanks
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 21:55
Joined
Aug 30, 2003
Messages
36,133
This line at the end appears to be reversed:

tmpstring = Cells(i, k) '.Value

That's setting the variable to the value in the cell again.
 

noboffinme

Registered User.
Local time
Today, 14:55
Joined
Nov 28, 2007
Messages
288
Thanks Paul

I thought I'd already tried that but you're right, that was it, Cheers
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 21:55
Joined
Aug 30, 2003
Messages
36,133
Happy to help.
 

Users who are viewing this thread

Top Bottom