2 more forumla explanations....

S

scotiajoe

Range("L1").Select
count2 = 1
For count1 = 1 To 900
Range(Cells(count2, 12), Cells(count2, 12)).Select
If ActiveCell.Value <> 0 Then
count2 = count2 + 1
Else: Selection.EntireRow.Delete
count2 = count2
End If
Next


Range("M1").Select
count2 = 1
For count1 = 1 To 900
Range(Cells(count2, 13), Cells(count2, 13)).Select
If ActiveCell.Value <> 0 Then
count2 = count2 + 1
Else: Selection.EntireRow.Delete
count2 = count2
End If
Next

Thank you
 
J

J.E. McGimpsey

Both snippets are rather slow methods of deleting lines with zero in
their respective columns.

The first Selects L1, then selects the 1st row of the 12th column
(which is also L1), checks if the value is 0. If not, it selects the
cell in the next row; if it is, it deletes the row. It will cycle
900 times.

It could be much more efficiently replaced by:

Dim i As Long
For i = 900 To 1 Step -1
If Cells(i, 12).Value = 0 Then Cells(i,12).EntireRow.Delete
next i

Note that no selections are necessary. It is necessary to go from
last row to first row since XL renumbers the rows when they're
deleted, but the loop variable is not adjusted.

A bit faster, since it deletes all the rows at once (and thus
doesn't have to go from high row number to low):

Dim i As long
Dim delRange As Range
For i = 1 to 900
With Cells(i, 12)
If .Value = 0 Then
If delRange Is Nothing Then
Set delRange = .Cells
Else
Set delRange = Union(delRange, .Cells)
End If
End If
End With
Next i
If Not delRange Is Nothing Then delRange.EntireRow.Delete

If both column L and column M are to be checked, instead of doing it
sequentially, you can do it at the same time:

Dim i As Long
Dim delRange As Range
For i = 1 to 900
With Cells(i, 12)
If .Value = 0 Or (.Offset(0, 1).Value = 0) Then
If delRange Is Nothing Then
Set delRange = .Cells
Else
Set delRange = Union(delRange, .Cells)
End If
End If
End With
Next i
If Not delRange Is Nothing Then delRange.EntireRow.Delete

and if there may be more or less than 900 rows to check, you can
replace

For i = 1 to 900

with

For i = 1 to Cells(Rows.Count, 12).End(xlUp).Row

to check all rows up to the last filled row in column L.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top