Next without For error

dubczech

Board Regular
Joined
Apr 29, 2008
Messages
52
I'm having a major brain-fart. I keep getting the Compile error: Next without For and it selects this loop:

For COMP2TotalRows = 2 To COMP2RowCount
If Range("L" & COMP2RowCount).Value = "CLOSEST" Then
Range("K" & COMP2RowCount).Value = COMP1Duns
Range("M" & COMP2RowCount).Value = Range("J" & COMP2RowCount).Value
Next COMP2TotalRows



The total Macro is here:


Sub DistanceCalculation()

Dim COMP1StartingPlace, COMP1Duns, COMP1Lat, COMP1Long As String
Dim COMP2RowCount, COMP1RowCount, COMP1TotalRows, COMP2TotalRows As Integer

Worksheets("COMP2 Only").Activate
COMP2RowCount = Range("A65536").End(xlUp).Row
Worksheets("Comp1 Only").Activate
COMP1RowCount = Range("A65536").End(xlUp).Row
For COMP1TotalRows = 2 To COMP1RowCount
COMP1Duns = Range("A" & COMP1TotalRows).Value
COMP1Lat = Range("H" & COMP1TotalRows).Value
COMP1Long = Range("I" & COMP1TotalRows).Value
Worksheets("COMP2 Only").Activate
Range("H2:H" & COMP2RowCount).Select
Selection.FormulaR1C1 = COMP1Lat
Range("I2:I" & COMP2RowCount).Select
Selection.FormulaR1C1 = COMP1Long
For COMP2TotalRows = 2 To COMP2RowCount
If Range("L" & COMP2RowCount).Value = "CLOSEST" Then
Range("K" & COMP2RowCount).Value = COMP1Duns
Range("M" & COMP2RowCount).Value = Range("J" & COMP2RowCount).Value
Next COMP2TotalRows
Next COMP1TotalRows

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi

You have no End if:

Rich (BB code):
For COMP2TotalRows = 2 To COMP2RowCount
  If Range("L" & COMP2RowCount).Value = "CLOSEST" Then
    Range("K" & COMP2RowCount).Value = COMP1Duns
    Range("M" & COMP2RowCount).Value = Range("J" & COMP2RowCount).Value
  End if
Next COMP2TotalRows
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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