The information in this article applies to:
SUMMARY
The way that Microsoft Excel 97 deletes cells in a For Each...Next loop in
differs from the way that earlier versions of Microsoft Excel delete
cells.
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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://www.microsoft.com/support/supportnet/overview/overview.asp Typing Sample DataTo use the macro in this article, type the following sample data in a worksheet:
Typing the Sample MacroIn a new macro module, type the following macro:
Behavior of the Sample Macro in Microsoft Excel 97When you run the macro DeleteCells 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:
When Microsoft Excel deletes row 3, all cells move up one row. For
example, cell A3 assumes the contents of cell A4, cell A4 assumes the
contents of cell A5, and so forth. After the For Each...Next loop evaluates
a cell, it evaluates the next cell; thus, when cells are shifted, they may
be skipped by the loop.
Behavior of the Sample Macro in Microsoft Excel 5.0 and 7.0When you run the macro DeleteCells 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:
When row 3 is deleted, all cells move up one row; and cell A3 assumes the
contents of cell A4, cell A4 assumes the contents of cell A5, and so
forth.
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; thus, 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:
The results of this macro in Microsoft Excel 97 and earlier versions are
as follows:
Additional Method for Using a Loop to Delete CellsThis is an alternate method to the method shown above. It will produce the same results.
REFERENCESFor more information about looping in a macro, click the Index tab in Visual Basic for Applications Help, type the following text loopsand then double-click the selected text to go to the "Looping Through Code" topic. Additional query words: XL97 8.0 8.00
Keywords : kbprg kbdta kbdtacode KbVBA xlvbmigrate |
Last Reviewed: November 9, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |