The information in this article applies to:
SUMMARY
The way that Microsoft Excel 2000 deletes cells in a For Each...Next loop in differs from the way that versions of Microsoft Excel earlier than Excel 97 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 a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/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 2000When you run the macro DeleteCells in Microsoft Excel 2000, 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 2000 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 2000, 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 2000 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, in the Visual Basic Editor, click
Microsoft Visual Basic Help on the Help menu, type Looping Through
Code in
the Office Assistant or the Answer Wizard, and then click Search to
view the topic. Additional query words:
Keywords : kbprg kbdta kbdtacode OffVBA KbVBA xlvbmigrate |
Last Reviewed: July 20, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |