Delete Rows not needed Macro

zone709

Well-known Member
Joined
Mar 1, 2016
Messages
2,129
Office Version
  1. 365
Platform
  1. Windows
I need to delete 2 kind of rows Macro. If H or I have a 0 or 0.00 in it. Then delete that row but i also need to delete one row above it also.
On Sheet2 i have Example the clean look and i am realizing if i can delete Column H or I with a 0 or 0.00 in them. Then also delete the row above it. Only Above not below.
You can see the rows with zeros i don't need. Then mostly not all have the same name above row. Deleting both i think would clean my whole sheet. (above only)

Test Macro Work.xlsx
ABCDEFGHI
1AMD
2Bill7/1/2024110Fine Street7/31/20247,500.007,500.00
3Total AMD7,500.007,500.00
4American
5Beac00
6American General
7Bill8/14/202434394Fine Street9/13/20245,197.915,197.91
8Total American General5,197.915,197.91
9Landscaping LLC
10Landscaping LLC00
11Corporation
12Corporation00
13Fuel Corpration
14Fuel Corpration00
15Arrow
16Bill7/1/202421049Fine Street7/31/202410,450.0010,450.00
17Bill8/1/202421170Fine Street8/31/202410,450.0010,450.00
Sheet1


Example Results

Test Macro Work.xlsx
ABCDEFGHI
1AMD
2Bill7/1/2024110Fine Street7/31/20247,500.007,500.00
3Total AMD7,500.007,500.00
4American General
5Bill8/14/202434394Fine Street9/13/20245,197.915,197.91
6Total American General5,197.915,197.91
7Arrow
8Bill7/1/202421049Fine Street7/31/202410,450.0010,450.00
9Bill8/1/202421170Fine Street8/31/202410,450.0010,450.00
Sheet2
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Maybe this
VBA Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
    For r = lr To 2 Step -1
        If Cells(r, "H").Value = 0 Or Cells(r, "I").Value = 0 Then
            Range(Cells(r, "H"), Cells(r - 1, "H")).EntireRow.Delete
         End If
    Next r
End Sub
 
Upvote 0
Hi thanks for the reply. After running this it comes out like this. Only trying to delete the 0 or 0.00 if in h and i and only one line above.
I highlighted in example 2 in yellow what i am trying to do. Any help would great thanks.

Test Macro Work.xlsx
ABCDEFGHI
1AMD
2Bill7/1/2024110Fine Street7/31/20247,500.007,500.00
3Bill8/14/202434394Fine Street9/13/20245,197.915,197.91
4Bill7/1/202421049Fine Street7/31/202410,450.0010,450.00
5Bill8/1/202421170Fine Street8/31/202410,450.0010,450.00
Sheet1



Example 2 only in yellow i want to delete 0 or 0.00 in h or i and one row above. Trying to save the vendor names in column B as you can see if only the yellow are deleted it will keep the vendor names in B.

Test Macro Work.xlsx
ABCDEFGHI
1AMD
2Bill7/1/2024110Fine Street7/31/20247,500.007,500.00
3Total AMD7,500.007,500.00
4American
5Beac00
6American General
7Bill8/14/202434394Fine Street9/13/20245,197.915,197.91
8Total American General5,197.915,197.91
9Landscaping LLC
10Landscaping LLC00
11Corporation
12Corporation00
13Fuel Corpration
14Fuel Corpration00
15Arrow
16Bill7/1/202421049Fine Street7/31/202410,450.0010,450.00
17Bill8/1/202421170Fine Street8/31/202410,450.0010,450.00
Sheet1
 
Upvote 0
Try this minor alteration to @Michael M 's code.
VBA Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
    For r = lr To 2 Step -1
        If (Cells(r, "H").Value = 0 Or Cells(r, "I").Value = 0) And _
            (Cells(r, "H").Value <> "" Or Cells(r, "I").Value <> "") Then
            Range(Cells(r, "H"), Cells(r - 1, "H")).EntireRow.Delete
         End If
    Next r
End Sub
 
Upvote 0
hi thanks for reply. It keep breaking at this spot.

VBA Code:
If (Cells(r, "H").Value = 0 Or Cells(r, "I").Value = 0) And _
            (Cells(r, "H").Value <> "" Or Cells(r, "I").Value <> "") Then

My cells have 0.00 i also tried changing them to 0 but same thing. Also the cells with 0 in it have formulas. I copied and paste that also with no formulas and it still breaks.
 
Upvote 0
What does "breaks" mean ?
It works fine on the sample data you provided in Post #1 and Post #3.
Did you do anything to the data before you created the XL2BB ? If you try it on the data that the XL2BB is based on does it work ?
 
Upvote 0
Here's a VBA macro that will delete rows based on your criteria. It checks columns H and I for values of 0 or 0.00, deletes the row containing the zero, and then deletes the row directly above it. You can place this in your VBA editor for your sheet:

vba
Copy code
Sub DeleteRowsWithZeroInHandI()
Dim ws As Worksheet
Dim i As Long
Set ws = ThisWorkbook.Sheets("Sheet1") 'Change to your sheet name if needed

' Start from the bottom row and work upwards
For i = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row To 2 Step -1
If (ws.Cells(i, 8).Value = 0 Or ws.Cells(i, 9).Value = 0 Or _
ws.Cells(i, 8).Value = 0.00 Or ws.Cells(i, 9).Value = 0.00) Then
ws.Rows(i).Delete
ws.Rows(i - 1).Delete ' Delete the row above
End If
Next i
End Sub
 
Upvote 0
What does "breaks" mean ?
It works fine on the sample data you provided in Post #1 and Post #3.
Did you do anything to the data before you created the XL2BB ? If you try it on the data that the XL2BB is based on does it work ?
Sorry i Figured out why it was Debugging. When i originally run the sheet before this. At the bottom in H and I i have #ref why it wasn't working. Anyway to ignore #ref. So it wont do this?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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