Article ID: 159915 - Last Review: November 23, 2006 - Revision: 2.2 XL97: How to Delete Cells with a For Each...Next LoopThis article was previously published under Q159915 For a Microsoft Excel 2002 version of this article, see 291300
(http://support.microsoft.com/kb/291300/EN-US/
)
.
For a Microsoft Excel 2000 version of this article, see 213544 (http://support.microsoft.com/kb/213544/EN-US/ ) . For a Microsoft Excel 98 version of this article, see 184378 (http://support.microsoft.com/kb/184378/EN-US/ ) . On This PageSUMMARY
Microsoft Excel 97 deletes cells in a For Each...Next loop in a different way from earlier versions of Microsoft Excel.
This article describes the differences and 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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites: Microsoft Certified Partners - https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104) Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice (http://support.microsoft.com/gp/advisoryservice) For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms) 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 Microsoft Excel 97When you run the DeleteCells macro in Microsoft Excel 97, 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 in Microsoft Excel 97 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 7.0When you run the DeleteCells macro in Microsoft Excel 5.0 and 7.0, the macro deletes all rows that contain an "x." The results of the macro in Microsoft Excel 5.0 or 7.0 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 Microsoft Excel 97, when the For Each...Next loop evaluates a cell in Microsoft Excel 5.0 and 7.0, it 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 shown above. It produces the same results.REFERENCES
For more information about looping in a macro, click the Index tab in
Visual Basic for Applications Help, type the following text
loops
and then double-click the selected text to go to the "Looping Through
Code" topic.
| Article Translations
|

Back to the top
