Next without For error

J

John Keturi

I get the following error, "Next without For". How do I change my code to
fix it?

'Format Colums which are John
For a = 4 To 31
If Cells(a, 1).Value = "John" Then
For b = 2 To 32
If Cells(2, b).Value = "Fri" Then
Cells(a, b).Select
With Selection.Interior
.ColorIndex = 0
.Pattern = xlGray8
.PatternColorIndex = xlAutomatic
End With
Next b
End If
Next a

MsgBox "Formatting Complete"
Exit Sub
Error:
Exit Sub
End Sub
 
N

Norman Jones

Hi John,

Responding to your error, and not otherwise looking at your code, you need
to insert:
End If
after your:
End With
 
B

billyb

Norman's answer is correct. This is a good example of why it's good t
use indentation in your code listing -- it makes it much easier to spo
these kinds of "out of balance" type errors. See indented code below
Also suggest that you not use VB keywords, like "Error" as labels
Finally, you likely realize that your your last "exit Sub" i
unnecessary but doesn't do any real harm; perhaps you included it jus
to make the code a bit more explicit, and thus clearer -- not a ba
idea.

'Format Colums which are John
Sub test()
On Error GoTo ErrHandler:
For a = 4 To 31
If Cells(a, 1).Value = "John" Then
For b = 2 To 32
If Cells(2, b).Value = "Fri" Then
Cells(a, b).Select
With Selection.Interior
.ColorIndex = 0
.Pattern = xlGray8
.PatternColorIndex = xlAutomatic
End With
End If
Next b
End If
Next a
MsgBox "Formatting Complete"
Exit Sub
ErrHandler:
Exit Sub
End Sub

Regards,
billly
 
B

billyb

I just realized that this board doesn't show the indentations in the
code. Sorry for that, but I think you'll get the idea.

billyb
 
Top