Wednesday, February 22, 2012

VBA: Deleting Rows Programmatically

When you need to delete rows in your spreadsheet programmatically, start from the last row and head towards the first one. This can be accomplished by using Step -1 in the For loop, see example below:


For lRow = lLastRow To lFirstRow Step -1
    ActiveSheet.Rows(lRow).Delete
Next lRow


If we would loop starting from the first row, our lRow counter would be pointing incorrectly at the moment when a first row gets deleted.

1 comment:

  1. Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.
    excel macro training courses london

    ReplyDelete