Page 21 of 24PREVNEXT

Get in the loop with Excel macros

A nested loop in action

This nested loop macro deletes rows with duplicate values in two columns.

With this macro, you could delete rows that contain duplicate values in columns D and F both. The power of this macro is that you can adapt it to act on duplicate values in other columns in your data.

Note    Take care when running this macro! It will delete data from your spreadsheet. And when you run a macro, there is no Undo. What's done is done. To be safe, copy your data first, and run this macro on the copy to test the results.

Nested loop macro code

The inside loop finds any rows that duplicate the starting row, and removes them. The outside loop moves the starting row down the selection, one row at a time, until each row has been compared with all those below it in the selection.

Page 21 of 24PREVNEXT