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

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

eirikdaude

Board Regular
Joined
Nov 26, 2013
Messages
60
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
 

eccydee

New Member
Joined
Dec 8, 2020
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,547
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

eccydee

New Member
Joined
Dec 8, 2020
Messages
4
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

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.
 

eccydee

New Member
Joined
Dec 8, 2020
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,547
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,528
Messages
5,636,847
Members
416,945
Latest member
Himu

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