Delete entire row only if certain conditions in all three rows

eccydee

New Member
Joined
Dec 8, 2020
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Hi I have a spreadsheet with product names listed in column A and quantities ordered in columns B, C and D. I am trying to write a code so that for each row where cells in columns B, C and D are all blank, the entire row is deleted. If any one of the cells has an entry then the entire row must remain. The top 3 rows are titles and I would also like the last row to remain. I would like this code to apply to all of the rows between the title and the last row.

Any suggestions would be most appreciated. Thanks in advance.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
VBA Code:
Option Explicit

Sub test()
    Dim i As Long
    Dim r As Range
   
    ' set the range to work on
    Set r = Sheet1.Range("A2:C" & Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row)
    ' skip the last row
    If r.Rows.Count > 1 Then Set r = r.Resize(r.Rows.Count - 1, r.Columns.Count)
   
    ' Loop over all rows, starting at the bottom (so we don't skip any). If you have a very large collection of products it may be more worthwhile to find all the rows to delete first, and then delete them in one operation
    For i = r.Rows.Count To 1 Step -1
        With r.Cells(i, 1)
            ' Check if all three cells are empty
            If Len(.Offset(0, 0)) = 0 And Len(.Offset(0, 1)) = 0 And Len(.Offset(0, 2)) = 0 Then
                ' delete the row
                .EntireRow.Delete
            End If
        End With
    Next i
End Sub
 
Upvote 0
VBA Code:
Option Explicit

Sub test()
    Dim i As Long
    Dim r As Range
  
    ' set the range to work on
    Set r = Sheet1.Range("A2:C" & Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row)
    ' skip the last row
    If r.Rows.Count > 1 Then Set r = r.Resize(r.Rows.Count - 1, r.Columns.Count)
  
    ' Loop over all rows, starting at the bottom (so we don't skip any). If you have a very large collection of products it may be more worthwhile to find all the rows to delete first, and then delete them in one operation
    For i = r.Rows.Count To 1 Step -1
        With r.Cells(i, 1)
            ' Check if all three cells are empty
            If Len(.Offset(0, 0)) = 0 And Len(.Offset(0, 1)) = 0 And Len(.Offset(0, 2)) = 0 Then
                ' delete the row
                .EntireRow.Delete
            End If
        End With
    Next i
End Sub
Thanks, I'll give it a try and report how it goes.
 
Upvote 0
Another option
VBA Code:
Sub eccydee()
   Dim UsdRws As Long
   
   With ActiveSheet
      UsdRws = Range("A" & Rows.Count).End(xlUp).Offset(-1).Row
      .Range("A3:D3").AutoFilter 2, ""
      .Range("A3:D3").AutoFilter 3, ""
      .Range("A3:D3").AutoFilter 4, ""
      .AutoFilter.Range.Offset(1).Resize(UsdRws - 4).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Solution
Another option
VBA Code:
Sub eccydee()
   Dim UsdRws As Long
  
   With ActiveSheet
      UsdRws = Range("A" & Rows.Count).End(xlUp).Offset(-1).Row
      .Range("A3:D3").AutoFilter 2, ""
      .Range("A3:D3").AutoFilter 3, ""
      .Range("A3:D3").AutoFilter 4, ""
      .AutoFilter.Range.Offset(1).Resize(UsdRws - 4).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
Thanks I'll try that too.
 
Upvote 0
Another option
VBA Code:
Sub eccydee()
   Dim UsdRws As Long
  
   With ActiveSheet
      UsdRws = Range("A" & Rows.Count).End(xlUp).Offset(-1).Row
      .Range("A3:D3").AutoFilter 2, ""
      .Range("A3:D3").AutoFilter 3, ""
      .Range("A3:D3").AutoFilter 4, ""
      .AutoFilter.Range.Offset(1).Resize(UsdRws - 4).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
This one was easier for me to incorporate into my existing code. Thanks so much.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,140
Members
448,551
Latest member
Sienna de Souza

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