VBA: Problems with For Loop (Amateur Coder)

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
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Code:
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
        
[B][COLOR="Red"]        End If
    
    Next i[/COLOR][/B]
    
End Sub
 
Last edited:
Upvote 0
By the way, variables i, j, a1, a2, b1, b2, c1 are Variant type.
Declaration should look like this:

Code:
    Dim i As Integer, j As Integer, lastrow As Integer
    Dim a1 As Long, a2 As Long, b1 As Long, b2 As Long, c1 As Long, c2 As Long
 
Last edited:
Upvote 0
Thanks a ton, Sektor. This seems to have resolved the issue.

A very very basic question - I want to include long decimals in my analysis, and I figured "Long" is just for integers.

What should I use for Decimals?

Thanks a lot!

Vishal
 
Upvote 0
Actually, you can't directly create variable of Decimal type. Here's workaround:

Code:
Sub TryDec()

    Dim dec As Variant
    Dim someVar As Double
    
    someVar = 10.02
    dec = CDec(someVar)
    
    ' Make sure dec is Decimal
    MsgBox "dec is of type: " & TypeName(dec)
    
End Sub
 
Upvote 0
vishal_c99,


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Sub Distance()
Dim i As Long, j As Long, lastrow As Long
Dim a1 As Double, a2 As Double, b1 As Double, b2 As Double, c1 As Double, c2 As Double
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
  End If
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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