Where is the problem on Next without For?

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

The following coding works fine, but once I change following code

"If CStr(d.Value) = ws.Name Then DelFlag = False"
into
"If CStr(d.Value) = ws.Name Then "
"DelFlag = False"
I would like to do it, so during the debugging process, I can see whether
this line is proceeded or not.
It pops up a message about Next without For,
Does anyone have any suggestions on where is the problem?

For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Date" Then
For Each d In MyRange
If CStr(d.Value) = ws.Name Then DelFlag = False
Exit For
End If
Next
If DelFlag Then
ws.Delete
End If
DelFlag = True
Next
 
hi Eric,

The problem is here:
If CStr(d.Value) = ws.Name Then DelFlag = False
Exit For
End If
It should be:
If CStr(d.Value) = ws.Name Then
DelFlag = False
Exit For
End If
 
When your if statement is in one line you can only have one statement
following the Then and you cannot have an End IF

Your code has to be like this
If CStr(d.Value) = ws.Name Then
DelFlag = False
Exit For
End If
 
Back
Top