vishal_c99
New Member
- Joined
- May 30, 2011
- Messages
- 2
Hi all,
I am struggling with a simple piece of code, which is as follows:
Sub Distance()
Dim i, j, lastrow As Integer
Dim a1, a2, b1, b2, c1, c2 As Long
Set j.Value = 2
lastrow = ActiveSheet.Cells(65536, 1).End(xlUp).Row
For i = 2 To lastrow
a1 = Cells(i, 1).Value
a2 = Cells(i, 2).Value
b1 = Cells(i + 1, 1).Value
b2 = Cells(i + 1, 2).Value
c1 = Abs(a1 - b1)
c2 = Abs(a2 - b2)
If c1 < 0.01 And c2 < 0.01 Then
Cells(j, 4) = a1
Cells(j, 5) = a2
Cells(j + 1, 4) = b1
Cells(j + 1, 5) = b2
j = j + 2
Next i
End If
End Sub
The runtime keeps giving me a compile error "Next without For". This is frustrating because I seem to have a Next after a For.
Please advise as I am new to VBA coding.
Thanks in advance,
Vishal
I am struggling with a simple piece of code, which is as follows:
Sub Distance()
Dim i, j, lastrow As Integer
Dim a1, a2, b1, b2, c1, c2 As Long
Set j.Value = 2
lastrow = ActiveSheet.Cells(65536, 1).End(xlUp).Row
For i = 2 To lastrow
a1 = Cells(i, 1).Value
a2 = Cells(i, 2).Value
b1 = Cells(i + 1, 1).Value
b2 = Cells(i + 1, 2).Value
c1 = Abs(a1 - b1)
c2 = Abs(a2 - b2)
If c1 < 0.01 And c2 < 0.01 Then
Cells(j, 4) = a1
Cells(j, 5) = a2
Cells(j + 1, 4) = b1
Cells(j + 1, 5) = b2
j = j + 2
Next i
End If
End Sub
The runtime keeps giving me a compile error "Next without For". This is frustrating because I seem to have a Next after a For.
Please advise as I am new to VBA coding.
Thanks in advance,
Vishal