XL97: Deleting Cells with a For Each...Next LoopLast reviewed: February 27, 1998Article ID: Q159915 |
The information in this article applies to:
SUMMARYThe 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. 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 examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.
Typing 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 Typing the Sample MacroIn a new macro module, type the following macro:
Sub DeleteCells() 'Loop through cells A1:A10 and delete cells that contain an "x." For Each c in Range("A1:A10") If c = "x" Then c.EntireRow.Delete Next End Sub 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:
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: 10When 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:
A1: a B1: 1 A2: b B2: 2 A3: c B3: 5 A4: d B4: 7 A5: e B5: 10When 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:
Sub DeleteCells2() Dim rng As Range Dim i As Integer, counter As Integer 'Set the range to evaluate to rng. Set rng = Range("A1:A10") 'initialize i to 1 i = 1 'Loop for a count of 1 to the number of rows 'in the range that you want to evaluate. For counter = 1 To rng.Rows.Count 'If cell i in the range contains an "x", 'delete the row. 'Else increment i If rng.Cells(i) = "x" Then rng.Cells(i).EntireRow.Delete Else i = i + 1 End If Next End SubThe results of this macro in Microsoft Excel 97 and earlier versions are as follows:
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 will produce the same results.
Sub DeleteCells3() Dim rng As Range, i As Integer 'Set the range to evaluate to rng. Set rng = Range("A1:A10") 'Loop backwards through the rows 'in the range that you want to evaluate. For i = rng.Rows.Count To 1 Step -1 'If cell i in the range contains an "x", delete the entire row. If rng.Cells(i).Value = "x" Then rng.Cells(i).EntireRow.Delete Next End Sub 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |