vba understand step meaning

Dancarro

Board Regular
Joined
Feb 23, 2013
Messages
65
Hi,

I am trying to understand what does the vba do in the highlighted section:

For i = LastRow To 2 Step -1
If WorksheetFunction.CountIf(Range(Cells(2, "C"), Cells(i, "C")), Cells(i, "C")) > 1
Then
Rows(i).Delete
End If
Next I

Kind Regards,
Dan
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
It is counting the number of times the values in column C of the current row appears in between row 2 and the current row in column C, to see if it is greater than 1 (if it is, it will delete that row).
So, it looks like this code is being used to delete duplicates (based on column C).
 
Upvote 0
What does 'To 2 Step -1' mean? Can you say 'For i = LastRow To 1'?

No; the default step for a loop variable is +1 unless otherwise specified. The run-time engine doesn't look to see what direction in needs to go.

It's behavior by design, and very appropriate.
 
Upvote 0
When deleting rows, it is always better to work backwards, from the bottom up. Otherwise, you may miss rows, as you delete and shift rows up into the range you have already checked.
 
Upvote 0
Hi Joe4 and shg

So what it means it goes down to the last row and goes upto to the second row then one row down; delete row. Then again, second row then one row down. Is my understanding correct?
 
Upvote 0
Hi Joe4 and shg

So what it means it goes down to the last row and goes upto to the second row then one row down; delete row. Then again, second row then one row down. Is my understanding correct?
No... it starts at the last row then after checking it (and possibly deleting it), the loop then goes to the row above that and checks it (and possibly deletes it), then the loop goes to the row above that and checks it (and possibly deletes it), and so on until the loop arrives at Row 2 which, after checking it (and possibly deleting it), it stop executing. In other words, let's say the last row was 100... the code would start the loop there and then move to Row 99 and then to Row 98 and then to Row 97 on upward until it gets to Row 2 where it ends.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top