VBA to delete rows based on zero in multiple columns

CenturyBreak

New Member
Joined
May 30, 2007
Messages
42
Hi all... again!

I'm looking for some efficient vba code that will delete rows based on the following criteria: all cells in said row in columns K through OA are zero (numerical value, not the string "zero").

I know I can loop through something like if-and-if-and for each row, but given that I'm trying to whittle down ~ 14,000 rows, that approach might be verrrrryyyyy sloooowwwww. ;)

Another factor is that for this month it will be from K to OA, while in following months the range will be wider. I've already built an index variable for the number of columns in the range to take that into account in future.

Thanks in advance! :)
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Code:
Public Sub DeleteZeroRows()
'
'define variables
'
Dim i As Long, lastrow As Long
'
'turn off screen updating and automatic calculation
'
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
'
'find last row of data in column K
'
lastrow = Range("K" & Rows.Count).End(xlUp).Row
'
'start at the bottom row
'
For i = lastrow To 1 Step -1
'
'if the sum of K to OA is zero then delete the row
'
    If Application.Sum(Range("K" & i & ":OA" & i)) = 0 Then
        Rows(i).Delete
    End If
Next i
'
'turn on screen updating and automatic calculation
'
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With
End Sub
 
Upvote 0
here's another one you may like to try
Code:
Sub testcode()
Dim u() As Variant, a As Range, rg As Range
Dim c1 As Long, c2 As Long, k As Long, j As Long
Set a = Range(Cells(1), Cells.SpecialCells(11))
ReDim u(1 To a.Rows.Count, 1 To 1)

c1 = a.Columns("K").Column
c2 = a.Columns("OA").Column
Set rg = a.Columns(c1).Resize(, c2 - c1 + 1)

a.Columns(c2).Offset(, 1).Insert
For Each q In rg.Rows
    k = k + 1
    If Application.CountIf(q, 0) = c2 - c1 + 1 Then
        u(k, 1) = 1
        j = j + 1
    End If
Next q
a.Columns(c2).Offset(, 1) = u
Set a = Range(Cells(1), Cells.SpecialCells(11))
a.Sort a.Columns(c2 + 1), 1, Header:=xlNo
a.Resize(j).Delete xlUp
a.Columns(c2 + 1).Delete xlToLeft

End Sub
 
Upvote 0
Thanks folks! (y)

I'll look at these this AyEm.

Code:
'if the sum of K to OA is zero then delete the row

Now why didn't I think of that?!?! :ROFLMAO:
 
Upvote 0
Thanks folks! (y)

I'll look at these this AyEm.

Code:
'if the sum of K to OA is zero then delete the row

Now why didn't I think of that?!?! :ROFLMAO:
Could it be because if you've got +1 in Col K, -1 in Col L and zeros elsewhere then you'll delete the row even though it doesn't meet your deletion criterion?

Or, there's very, very, many ways that a set of numbers can sum to zero without each one being zero.
 
Upvote 0
Could it be because if you've got +1 in Col K, -1 in Col L and zeros elsewhere then you'll delete the row even though it doesn't meet your deletion criterion?

Or, there's very, very, many ways that a set of numbers can sum to zero without each one being zero.

Of course... but chuckles read my mind without me knowing it: with this set of data, it's highly improbable (though minutely possible) that there will be any negative values. ;)
 
Upvote 0
Not sure what you're analyzing or whether it matters if you have accidental deletions - but it's generally better to assume that the wrong thing will happen, regardless of how improbable it is.
 
Upvote 0
Of course... but chuckles read my mind without me knowing it: with this set of data, it's highly improbable (though minutely possible) that there will be any negative values. ;)
Hey,

It helps in this business not to have to be too much of a mindreader (just joking :ROFLMAO:)

But if you like sums rather than counting, it would be generally safer (here I agree with ret44's comment) to calculate the sum of squares of your numbers, or the sum of their absolute values.

But since your main original aim was to find efficient codes, you might still like to try the one I posted above. And if you have a strong preference for whatever reason for summing rather than counting, you can change this line
Rich (BB code):
If Application.CountIf(q, 0) = c2 - c1 + 1 Then
to
Rich (BB code):
If Application.SumSq(q) = 0 Then
or
Rich (BB code):
If Application.Sum(Abs(q)) = 0 Then
or even, if you're really set on summing the individual values as they stand
Rich (BB code):
If Application.Sum(q) = 0 Then
... of course it's entirely your problem, but if it were me I'd really avoid the last one.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,203,073
Messages
6,053,381
Members
444,660
Latest member
Mingalsbe

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top