VBA: Delete rows if...

cdrobinson83

New Member
Joined
May 3, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to write something that will delete rows based on certain criteria. Based on the below, if "C" is "EXCHANGE_REPORTING_NUMBER" and "F" AND "G" are both zero, I want to delete that row and any row beneath it that shares the same details in "B" and "D". So for this case, the end result I'm looking for is for rows 2, 3, and 4 to be deleted and rows 5, 6, 7, and 8 to remain untouched. Is anyone able to help with this?

1676593960970.png
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi,
You can turn on your macro recorder .... and go through your process once ... using Autofilter :)
 
Upvote 0
Hi,
You can turn on your macro recorder .... and go through your process once ... using Autofilter :)
There is a little more to it than that, the section header row identifies which section to delete but the whole section then needs to be deleted.
Autofilter is only going to delete the section header.
 
Upvote 0
On a copy of your workbook give this a try.

VBA Code:
Sub DeleteZeroSections()

    Dim shtData As Worksheet
    Dim rngData As Range
    Dim arrData As Variant
    Dim rowLast As Long, colLast As Long, colNext As Long
    Dim colIdxLast As Long
    Dim i As Long
    
    Set shtData = ActiveSheet
    With shtData
        rowLast = .Cells(Rows.Count, "B").End(xlUp).Row
        colLast = .Cells(1, Columns.Count).End(xlToLeft).Column
        colNext = colLast + 1
        Set rngData = .Range(.Cells(2, "A"), .Cells(rowLast, colLast))
        arrData = rngData.Value2
        ReDim Preserve arrData(1 To UBound(arrData, 1), 1 To UBound(arrData, 2) + 1)    ' This leaves the additional column empty
    End With
    
    colIdxLast = UBound(arrData, 2)
    For i = 1 To UBound(arrData)
        If arrData(i, 3) = "EXCHANGE_REPORTING_NUMBER" Then
            If CCur(arrData(i, 6)) = 0 And CCur(arrData(i, 7)) = 0 Then
                arrData(i, colIdxLast) = 1      ' Flag rows for deletion
            End If
        End If
        If i <> 1 Then
            If arrData(i, 2) = arrData(i - 1, 2) And arrData(i, 4) = arrData(i - 1, 4) Then
                arrData(i, colIdxLast) = arrData(i - 1, colIdxLast)    ' Flag rows for deletion
            End If
        End If
    Next i
    
    With rngData.Columns(colNext).Resize(, 1)
        .Value = Application.Index(arrData, 0, colIdxLast)
        .SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow.Delete
    End With
        
End Sub
 
Upvote 0
Solution
On a copy of your workbook give this a try.

VBA Code:
Sub DeleteZeroSections()

    Dim shtData As Worksheet
    Dim rngData As Range
    Dim arrData As Variant
    Dim rowLast As Long, colLast As Long, colNext As Long
    Dim colIdxLast As Long
    Dim i As Long
   
    Set shtData = ActiveSheet
    With shtData
        rowLast = .Cells(Rows.Count, "B").End(xlUp).Row
        colLast = .Cells(1, Columns.Count).End(xlToLeft).Column
        colNext = colLast + 1
        Set rngData = .Range(.Cells(2, "A"), .Cells(rowLast, colLast))
        arrData = rngData.Value2
        ReDim Preserve arrData(1 To UBound(arrData, 1), 1 To UBound(arrData, 2) + 1)    ' This leaves the additional column empty
    End With
   
    colIdxLast = UBound(arrData, 2)
    For i = 1 To UBound(arrData)
        If arrData(i, 3) = "EXCHANGE_REPORTING_NUMBER" Then
            If CCur(arrData(i, 6)) = 0 And CCur(arrData(i, 7)) = 0 Then
                arrData(i, colIdxLast) = 1      ' Flag rows for deletion
            End If
        End If
        If i <> 1 Then
            If arrData(i, 2) = arrData(i - 1, 2) And arrData(i, 4) = arrData(i - 1, 4) Then
                arrData(i, colIdxLast) = arrData(i - 1, colIdxLast)    ' Flag rows for deletion
            End If
        End If
    Next i
   
    With rngData.Columns(colNext).Resize(, 1)
        .Value = Application.Index(arrData, 0, colIdxLast)
        .SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow.Delete
    End With
       
End Sub

Thank you so much! This worked perfectly.
 
Upvote 0

Forum statistics

Threads
1,214,405
Messages
6,119,323
Members
448,887
Latest member
AirOliver

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