Select the product you need help with
How to delete cells by using a "For Each...Next" loopArticle ID: 291300 - View products that this article applies to. This article was previously published under Q291300
For a Microsoft Excel 2000 version of this article, see 213544
(http://support.microsoft.com/kb/213544/
)
.
For a Microsoft Excel 98 version of this article, see 184378
(http://support.microsoft.com/kb/184378/
)
.
For a Microsoft Excel 97 version of this article, see 159915
(http://support.microsoft.com/kb/159915/
)
.
On This PageSUMMARY
Microsoft Excel 2002 and later versions of Excel delete cells in a "For Each...Next" loop in a different way than Microsoft Excel 97 and earlier versions of Excel do. This article describes the differences, and it provides a Visual Basic for Applications macro example that illustrates how to delete cells in a loop. MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure. However, they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. Sample dataTo use the macro in this article, type the following sample data in a worksheet:A1: a B1: 1 A2: b B2: 2 A3: x B3: 3 A4: x B4: 4 A5: c B5: 5 A6: x B6: 6 A7: d B7: 7 A8: x B8: 8 A9: x B9: 9 A10: e B10: 10 Sample macroIn a new macro module, type the following macro.Behavior of the sample macro in Excel 2002 and in later versions of ExcelWhen you run the DeleteCells macro in Excel 2002 and in later versions of Excel, only rows 3, 6 and 8 are deleted. Although rows 4 and 9 contain an "x" in column A, the macro does not delete the rows. The results of the macro are as follows:A1: a B1: 1 A2: b B2: 2 A3: x B3: 4 A4: c B4: 5 A5: d B5: 7 A6: x B6: 9 A7: e B7: 10 Behavior of the sample macro in Microsoft Excel 5.0 and Microsoft Excel 7.0When you run the DeleteCells macro in Excel 5.0 and in Excel 7.0, the macro deletes all rows that contain an "x." The results of the macro are as follows:A1: a B1: 1 A2: b B2: 2 A3: c B3: 5 A4: d B4: 7 A5: e B5: 10 However, unlike the behavior of the loop in Excel 2002 and in later versions of Excel, when the "For Each...Next" loop evaluates a cell in Excel 5.0 and in Excel 7.0, the loop reevaluates the cell if it is deleted in the loop. Therefore, the cells are not skipped. Recommended method for using a loop to delete cellsUse the following macro when you want to use a loop to delete cells:A1: a B1: 1 A2: b B2: 2 A3: c B3: 5 A4: d B4: 7 A5: e B5: 10 Additional method for using a loop to delete cellsThis is an alternate method to the method that is shown above. This method produces the same results.Properties | Article Translations
|


Back to the top








