The array will automatically get the same dimensions as the range (number of rows and columns) Copy the range to a two-dimensional array in one (very fast)operation.I have no idea if anybody ever needs to do this, but because it is simple, it is good for showing the technique. The following example is fairly simple: It removes every second row in a range.
How to delete certain rows in excel vba code#
auto filtering), because no VBA code will ever match the speed of those but if you have special needs, VBA can be a great help. If the task is sorting or filtering, you should always use Excel's built in functions (e.g. log files or reports) and often need to "weed" the data using certain criteria. The method isn't suitable for all tasks, but I think that many people work with very big tables in Excel (e.g. You get the speed increase, because there is a large overhead writing to and from a range in a spreadsheet, whereas it is very fast to operate on a virtual table (the array). Once you have finished operating on the array, you copy the array to the spreadsheet in one lightning fast operation. If you work with big tables/ranges, you can usually speed up the code a lot, if you copy the range to an array. You can also download a spreadsheet with the examples.
You can use other criteria, and the task doesn't have to be the removal of rows - that is just the objective for my examples.
How to delete certain rows in excel vba how to#
The first example shows how to remove every second row, and the next how to delete rows going from the last row upwards, if the row above has identical/duplicate values in selected columns. rows, but it is tricky, because your range suddenly changes, and if for instance you remove the row you are in, you will suddenly be in the row that was the row below just a moment ago! It is both easier and faster with an array. You CAN loop through a range and remove or insert e.g. This could be deleting rows in a range (table) or other operations. This page shows examples of how you can speed things up a lot by using arrays. Many, who start using VBA in Excel, quickly learn how to use ranges, because they are really handy and relatively easy to understand and use. Arrays and ranges in Excel VBA - delete rows