VBA to Delete Rows based on value of a cell

Aggie2014

New Member
Joined
Dec 26, 2019
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

As part of a bigger macro, I'm adding code that will delete rows of data if the value in column O is 0 and I can't seem to get it to function correctly. Data in column O is a dummy variable that's been created based on data in other columns and is posted as a value, rather than formula. I'm not getting any errors when I run the macro, however the cells with 0 value in column O do not get deleted.

The code that I'm using for the deletion is as follows:

VBA Code:
For w = Range("C" & Rows.Count).End(xlUp).Row to 7 Step -1
If Range("O7" & w).Value = "0" Then
Rows(w).EntireRow.Delete
End If
Next w

Am I not seeing something that's causing it not to work properly?
 
@DanteAmor

Thanks for the code! I've tried using autofiller, but because I have blank rows separating my data subsets, the filter would not go all the way to the bottom of the dataset, but stop at the first blank separator row. Could the proposed code be augmented to go to the bottom of the set, rather than stopping at the first blank?

Tryt this, It should work even with empty cells in column O or column A
VBA Code:
Sub Macro1()
  Range("A7", Cells(Rows.Count, "O").End(xlUp)).AutoFilter Field:=15, Criteria1:=0
  ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.Delete
  ActiveSheet.ShowAllData
End Sub
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
So you know how you don't see some things on a big dataset until you look at it a few times or at a later time?

I just noticed that the deletion code also deletes the title rows of the data subsets that do not have anything in the dummy variable column. It's the weirdest thing, because it leave the blank rows alone, but if there is a title in column C and no data in the rest of the cells, it deletes it. These titles will be in different rows depending on the size of the dataset and need to be included. Is there a way to tweak the code not to delete these?
 
Upvote 0
Sorry, I'm trying to reproduce examples of what you mention and Michael's macro in post #3 and my macro in post #11 delete the data if in column O it has zero (0).
Maybe I'm missing something.
You could paste a sample of your data here where the macro is deleting the records it shouldn't.
 
Upvote 0
Hi,

Here's a reproduced sample of the dataset. The math doesn't work on the over/under and calculations, but the formatting and totals and subset headers are accurate. The calculated dummy variable which in the example is in column N is what I'm using to filter the data. If the value in that column is 0, then the row needs to be deleted; if it's not zero or blank then it needs to stay in.

Book1
ABCDEFGHIJKLMN
1
2
3Fund 1
4
5AccountDescriptionPYE ActualPYTD ActualBudgetActualDifferenceYear End ProjectionOver/underProposed Budgetover/underProjection WarningCommentsDummy Variable
6
71revenue 11010101010101010103
82revenue 22020202020202020204
93revenue 33030303030303030300
104revenue 41414141414141414140
11***TOTAL REVENUE***7474747474747474744
12
13PERSONNEL SERVICES
141Account 11010101010101010100
152Account 22020202020202020200
163Account 33030303030303030303
174Account 41414141414141414144
18***TOTAL PERSONNEL SERVICES***7474747474747474745
19
20
21SUPPLIES
221Account 11010101010101010105
232Account 22020202020202020204
243Account 33030303030303030303
254Account 41414141414141414140
26***TOTAL SUPPLIES***7474747474747474748
27
28
29PERSONNEL CAPITAL OUTLAY
301Account 11010101010101010100
312Account 22020202020202020200
323Account 33030303030303030305
334Account 41414141414141414145
34***TOTAL CAPITAL OUTLAY***7474747474747474745
Sheet1
Cell Formulas
RangeFormula
C11:K11, C34:K34, C26:K26, C18:K18C11=SUM(C7:C10)
 
Upvote 0
Here's the code that's in the macro currently:

VBA Code:
Dim lastRow2 As Long
lastRow2 - Cells(Rows.Count. "H").End(xlUp).Row
With Range("N7:N" & lastRow2)
.Formula = "=D7+E7+F7+G7+i7+k7"
.Value = .Value
End With

With Range("N7", Cells(Rows.Count, "N").End(xlUp))
.Replace 0, "#N/A", xlWhole, , False, , False, False
Columns("N").SpecialCalls(XlConstants, xlErrors).EntireRow.Delete
End With
 
Upvote 0
Sorry for the multiple posts: the data that's getting deleted is in rows 13, 21, and 29 in the example above.
 
Upvote 0
To bring this to a close, I made the macro work by hard posting values into the subset header rows to trick the macro into not deleting them. Since it's a dummy variable, the entire column gets deleted as part of the macro once the zero cells get deleted. It's not the most elegant way, but it got me through the roadblock with the macro.

Thank you all very much for your assistance and suggestions on this.
 
Upvote 0

Forum statistics

Threads
1,223,977
Messages
6,175,753
Members
452,667
Latest member
vanessavalentino83

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