View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson[_5_] Jim Thomlinson[_5_] is offline
external usenet poster
 
Posts: 486
Default General programming help

Create a new range object to accumulate the cells that you find. Note that
the union will fail unless rngToDelete is something other than nothing, so
you need to check this each time through the loop, but it will only execute
once. If you are looking to improve the efficiency you might want to consider
changing the used range of column A to the blank cells in column A. Depending
how much data you have and how it looks this could make for a huge
improvement in the speed and efficiency...

Dim rngToDelete As Range

Set ColA = Intersect(Sheet1.Columns("A"), Sheet1.UsedRange)
For Each cell In ColA
If cell.Value = Empty And Not cell.Offset(0, 1).Value = "Weekend Diff"
Then
If rngToDelete Is Nothing Then Set rngToDelete = cell
Set rngToDelete = Union(rngToDelete, cell)
End If
Next cell
rngToDelete.EntireRow.Delete shift:=xlUp

To improve efficiency

Dim rngToDelete As Range
Dim colA As Range
On Error Resume Next
Set colA = Sheet1.Columns("A").SpecialCells(xlBlanks)
On error goto 0
If colA Is Nothing Then Exit Sub

For Each cell In colA
If Not cell.Offset(0, 1).Value = "Weekend Diff" Then
If rngToDelete Is Nothing Then Set rngToDelete = cell
Set rngToDelete = Union(rngToDelete, cell)
End If
Next cell
rngToDelete.EntireRow.Delete shift:=xlUp
--
HTH...

Jim Thomlinson


"Erik" wrote:

I'm trying to use the code below to delete rows from a sheet. The problem is
that as it deletes a row, it then skips the next one. How can I create a
list of the rows to be deleted and delete them all at once after the For loop?

Set ColA = Intersect(Sheet1.Columns("A"), Sheet1.UsedRange)
For Each cell In ColA
If cell.Value = Empty And Not cell.Offset(0, 1).Value = "Weekend Diff"
Then
cell.EntireRow.Delete shift:=xlUp
End If
Next cell