Follow up For loop exiting out early

  • Thread starter Thread starter jimmy
  • Start date Start date
J

jimmy

I have this piece of code which jouni helped out with.
the code doesnt really do anything because the imbedded
for loop cuts out of the outer for loop and exiting the
macro. it exits out before executing the most important
command. i dont know why....

For Each ws In ActiveWorkbook.Sheets
ws.Activate
If Selection.Rows.Count > 1 Then
Set Rng = Selection
Else
Set Rng = ActiveSheet.UsedRange.Rows
End If


For r = Rng.Rows.Count To 1 Step -1
V = Rng.Cells(r, 1).Value
intCounter = 0

For Each ws2 In ActiveWorkbook.Sheets
intCounter = intCounter +
Application.WorksheetFunction.CountIf(ws2.Columns(1), V)
Next ws2

If intCounter > 1 Then
Rng.Rows(r).EntireRow.Delete
N = N + 1
End If

Next r


Next ws


the inner for loop is the one that cuts out, it goes
through properly like its supposed to but then it skips
over the if statement and goes completely out of the outer
for outside the Next ws command. Any ideas why this is
happening?
 
Jimmy,

you set intCounter is set to 0, then add 1 to it, so it
is equal to 1. Then the inner loop checks to see if it is
greater than 1. Well... it is not, so it moves on.

I think you could just change the line:
If intCounter > 1 Then
to:
If intCounter > 0 Then

but maybe I missed something.

Claire
BestMeasure @ hotmail . com
 
Back
Top