PDA

View Full Version : [SOLVED:] Next without For Compile Error



Poundland
08-05-2015, 01:36 AM
Hi Guys,

Can anybody tell me why I am getting a Next without For Compile Error on the below code, as far as I can see each For Statement has a Next Statement to match!

Thanks in advance..


Sub results()
Sheets("Results").Select
Cells(2, 1).Select
Sheets("All").Select
Cells(5, 2).Select
Selection(Selection.End(xlToRight)).Select
CountMS = Selection.Count + 1

Cells(6, 1).Select
Selection(Selection.End(xlDown)).Select
CountStore = Selection.Count + 6
Cells(6, 1).Select
c = ActiveCell.Column
For a = 6 To CountStore
Cells(a, c).Select
Store = ActiveCell.Value
R = ActiveCell.Row
For b = 2 To CountMS
Cells(R, CountMS).Select
If ActiveCell.Value > 0 Then
GoTo AA
Else
Cells(5, CountMS).Select
Master = ActiveCell.Value
Sheets("Results").Select
ActiveCell.Value = Store
Selection.Offset(0, 1).Select
ActiveCell.Value = Master
Selection.Offset(1, -1).Select
Sheets("All").Select
AA:
Next b
End If
Next a

Poundland
08-05-2015, 01:53 AM
OK, I have changed my code so that it starts to run, but when I get to the first Cells selection line on the All sheet, I get a "Application-defined or object-defined error"

The cells statement is referencing within a Pivot Table if that helps any..


Sub results()
Sheets("Results").Select
Cells(2, 1).Select
Sheets("All").Activate
Cells(5, 2).Select
Range(Selection, Selection.End(xlToRight)).Select
CountMS = Selection.Count + 1

Cells(6, 1).Select
Range(Selection, Selection.End(xlDown)).Select
CountStore = Selection.Count + 6
Cells(6, 1).Select
c = ActiveCell.Column
For a = 6 To CountStore
Cells(a, c).Select
Store = ActiveCell.Value
R = ActiveCell.Row
For b = 2 To CountMS
Cells(R, CountMS).Select
If ActiveCell.Value > 0 Then
GoTo AA
Else
Cells(5, CountMS).Select
Master = ActiveCell.Value
Sheets("Results").Select
ActiveCell.Value = Store
Selection.Offset(0, 1).Select
ActiveCell.Value = Master
Selection.Offset(1, -1).Select
Sheets("All").Select
AA:
End If
Next b
Next a
End Sub

mancubus
08-05-2015, 02:10 AM
it seems you replaced Next b with End If.

btw, you don't need all those "select"s.

try coding like:



Sub results()

With Sheets("All")
CountMS = .Range(.Range("B5"), .Range("B5").End(xlToRight)).Count + 1
CountStore = .Range(.Range("A6"), .Range("A6").End(xlDown)).Count + 6
For a = 6 To CountStore
Store = .Cells(a, 1).Value
For b = 2 To CountMS
If Cells(i, CountMS).Value <= 0 Then
Master = .Cells(5, CountMS).Value
Sheets("Results").Cells(b, 1).Value = Store
Sheets("Results").Cells(b, 2).Value = Master
End If
Next b
Next a
End With

End Sub

Poundland
08-05-2015, 03:46 AM
Thanks Mancubus for your help, much appreciated buddy.