VBA code to delete rows IF cell contains certain text AND other cell = 0

cash2021

New Member
Joined
Dec 5, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I've tried this about a million different ways & cannot seem to get it to work. I'm an amateur when it comes to VBA. My goal is to delete entire rows if value in column G is "0" & column L is "FALSE" to regularly clean up this spreadsheet & keep it from becoming overwhelming. For context - columns in yellow are formulas that calculate based on cell values in columns A:H, & since the ultimate goal is to delete rows with dates before yesterday AND column G = 0, I've also tried using the date/now functions with no luck (hence moving on to the true/false column, since I already had it sitting there to filter a pivot table). I've pasted the code I'm attempting to use below along with a screenshot of what the data looks like. Little help would be awesome!

1607225400876.png


VBA Code:
Sub DeleteRows()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
 Dim LR As Long, i As Long
LR = Range("G" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
   If (Range("G" & i) = "0" And Range("L" & i) = "FALSE") Then Rows(i).Delete
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,477
Office Version
  1. 2013
Platform
  1. Windows
So try this looks in column A for Date less the yesterday and "0" in column
VBA Code:
Sub Delete_Rows()
'Modified 12/6/2020  5:03:49 AM  EST
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row

For i = Lastrow To 2 Step -1
    If Cells(i, 1).Value < Date - 1 And Cells(i, "G").Value = "0" Then Rows(i).Delete
Next
Application.ScreenUpdating = True
End Sub
 
Solution

cash2021

New Member
Joined
Dec 5, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
So try this looks in column A for Date less the yesterday and "0" in column
VBA Code:
Sub Delete_Rows()
'Modified 12/6/2020  5:03:49 AM  EST
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row

For i = Lastrow To 2 Step -1
    If Cells(i, 1).Value < Date - 1 And Cells(i, "G").Value = "0" Then Rows(i).Delete
Next
Application.ScreenUpdating = True
End Sub
INCREDIBLE! This worked beautifully - looks like the issue was referencing the range rather than the cells themselves? Can't thank you enough! :)
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,477
Office Version
  1. 2013
Platform
  1. Windows
INCREDIBLE! This worked beautifully - looks like the issue was referencing the range rather than the cells themselves? Can't thank you enough! :)
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
 

Watch MrExcel Video

Forum statistics

Threads
1,128,119
Messages
5,628,799
Members
416,340
Latest member
PJB1102

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
Top