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?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi Aggie2014,

Try this:

VBA Code:
Option Explicit
Sub Macro1()
    
    Dim w As Long
    
    Application.ScreenUpdating = False

    For w = Range("O" & Rows.Count).End(xlUp).Row To 7 Step -1
        If IsNumeric(Range("O" & w)) = True And Val(Range("O" & w)) = 0 Then
            Rows(w).EntireRow.Delete
        End If
    Next w
    
    Application.ScreenUpdating = True
    
End Sub

Regards,

Robert
 
Upvote 0
Maybe this way....

VBA Code:
Sub MM1()
  With Range("O7", Cells(Rows.Count, "O").End(xlUp))
    .Replace 0, "#N/A", xlWhole, , False, , False, False
    Columns("O").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
  End With
End Sub
 
Upvote 0
Hi Michael,

Hope you're well. Happy new year!!

Nice code - the only "issue" is that cells that have zero as the result of a formula (say =6-6) will not be deleted. Thus may not be an issue of course.

Hope you enjoy the Elvis festival in just over a week :cool:

Cheers,

Robert
 
Upvote 0
the only "issue" is that cells that have zero as the result of a formula (say =6-6) will not be deleted.
Not the only issue, as
- rows with cells that already contained a constant #N/A value would also be deleted,
- rows above row 7 could also be deleted
but these also ..
may not be an issue of course.

BTW, @Trebor76 & @Aggie2014, since Rows(w) is already an entire row, that does not need to be re-specified.
Rich (BB code):
Rows(w).EntireRow.Delete


@Aggie2014
  1. About how big (number of rows) is your original data set?
  2. Can cells in column O be empty or contain "" and, if so, should they be deleted too?
 
Upvote 0
Hmm.
I read the post to be...
VBA Code:
and is posted as a value, rather than formula
 
Upvote 0
Here is another macro for you to consider:

VBA Code:
Sub Macro1()
  Range("A1:O" & Range("O" & Rows.Count).End(xlUp).Row).AutoFilter Field:=15, Criteria1:=0
  ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.Delete
  ActiveSheet.ShowAllData
End Sub
 
Upvote 0
Not the only issue, as
- rows with cells that already contained a constant #N/A value would also be deleted,
- rows above row 7 could also be deleted
but these also ..

BTW, @Trebor76 & @Aggie2014, since Rows(w) is already an entire row, that does not need to be re-specified.
Rich (BB code):
Rows(w).EntireRow.Delete


@Aggie2014
  1. About how big (number of rows) is your original data set?
  2. Can cells in column O be empty or contain "" and, if so, should they be deleted too?

@Peter_SSs

This dataset is a financial line item summary separated by categories i.e. revenues, supplies, capital outlay, etc, so the number of lines will vary from around 15 to over a 100 depending on the fund. Because they are separated by the categories and have blank rows between each of the subsets deleting lines that have blanks would be an issue. The dummy variable that's in column N pulls out the rows that have 0 in several other columns, through a sum function and then posted as values, and thus are extraneous to the analysis. That's been the bit I'm struggling with because I need the rows that are blank to remain in place.
 
Upvote 0
Here is another macro for you to consider:

VBA Code:
Sub Macro1()
  Range("A1:O" & Range("O" & Rows.Count).End(xlUp).Row).AutoFilter Field:=15, Criteria1:=0
  ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.Delete
  ActiveSheet.ShowAllData
End Sub

@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?
 
Upvote 0
Maybe this way....

VBA Code:
Sub MM1()
  With Range("O7", Cells(Rows.Count, "O").End(xlUp))
    .Replace 0, "#N/A", xlWhole, , False, , False, False
    Columns("O").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
  End With
End Sub

@Michael M

Thanks! This is precisely what I needed! I dropped it into my macro and it worked like a charm
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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