I
IanKR
I have the block of code below in a Sub that tidies up a copied worksheet
by removing unused rows. It’s contained in a With … End With block that
refers to the copied sheet. I’d like to know how the Exit For statement is
handled if it’s in a For ... Next block that is nested within another For
… Next block. I.e. will it cause the code to exit only the innerFor …
Next block and proceed with the
If VacRowClear = True Then
statement, or will it exit the outer For … Next block?
I use nested For … Next blocks quite a lot in my code; are they considered
bad programming practice?
'delete Vacancy block rows if clear
If VacancyStartRow < VacancyEndRow Then
For r = VacancyEndRow To VacancyStartRow Step -1
'assume clear
VacRowClear = True
'test whether first 9 cells are all clear
For c = 1 To 9
If .Cells(r, c).Value <> "" Then 'if any cell not clear....
VacRowClear = False ' ... reset Boolean
Exit For ‘<---- will this statement exit only the inner
‘ For ... Next block, or the outer one as well?
End If
Next c
'if all 9 first cells are clear, delete row
If VacRowClear = True Then
.Rows(r).Delete
End If
Next r
End If
by removing unused rows. It’s contained in a With … End With block that
refers to the copied sheet. I’d like to know how the Exit For statement is
handled if it’s in a For ... Next block that is nested within another For
… Next block. I.e. will it cause the code to exit only the innerFor …
Next block and proceed with the
If VacRowClear = True Then
statement, or will it exit the outer For … Next block?
I use nested For … Next blocks quite a lot in my code; are they considered
bad programming practice?
'delete Vacancy block rows if clear
If VacancyStartRow < VacancyEndRow Then
For r = VacancyEndRow To VacancyStartRow Step -1
'assume clear
VacRowClear = True
'test whether first 9 cells are all clear
For c = 1 To 9
If .Cells(r, c).Value <> "" Then 'if any cell not clear....
VacRowClear = False ' ... reset Boolean
Exit For ‘<---- will this statement exit only the inner
‘ For ... Next block, or the outer one as well?
End If
Next c
'if all 9 first cells are clear, delete row
If VacRowClear = True Then
.Rows(r).Delete
End If
Next r
End If