Delete rows with reference

Sathisc

Board Regular
Joined
Jul 29, 2008
Messages
103
Hi,

I have a data consists of 3 column as given below

[TABLE="width: 192"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]10252554[/TD]
[TD]SEA[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]10252554[/TD]
[TD]LSF[/TD]
[TD]MC[/TD]
[/TR]
[TR]
[TD]10252554[/TD]
[TD]DGP[/TD]
[TD]MC[/TD]
[/TR]
[TR]
[TD]10252554[/TD]
[TD]BOO[/TD]
[TD]MC[/TD]
[/TR]
[TR]
[TD]10252554[/TD]
[TD]TSD[/TD]
[TD]BTO[/TD]
[/TR]
[TR]
[TD]10252554[/TD]
[TD]THD[/TD]
[TD]BTO[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]SEA[/TD]
[TD]P[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]LSF[/TD]
[TD]MC[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]DGP[/TD]
[TD]MC[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]BOO[/TD]
[TD]MC[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]TSD[/TD]
[TD]BTO[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]THD[/TD]
[TD]BTO[/TD]
[/TR]
</tbody><colgroup><col span="3"></colgroup>[/TABLE]


scenario - If Column B consists of "SEA" and Colum C with "C" the rows(entire) which has the number 10252554 needs to get deleted.

Help is much appreciated

Output required

[TABLE="width: 192"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]SEA[/TD]
[TD]P[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]LSF[/TD]
[TD]MC[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]DGP[/TD]
[TD]MC[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]BOO[/TD]
[TD]MC[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]TSD[/TD]
[TD]BTO[/TD]
[/TR]
[TR]
[TD]10252548[/TD]
[TD]THD[/TD]
[TD]BTO[/TD]
[/TR]
</tbody><colgroup><col span="3"></colgroup>[/TABLE]
 
One question that will help me formulate a plan...
Is there any possibility for duplicates with the "SEA/C" combination?
Meaning, could you have two (or more) rows that have "SEA/C/10252554"?
If there is no possibility of duplicates, it will make it much easier.
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
One question that will help me formulate a plan...
Is there any possibility for duplicates with the "SEA/C" combination?
Meaning, could you have two (or more) rows that have "SEA/C/10252554"?
If there is no possibility of duplicates, it will make it much easier.

the combination will appear only once and there is no duplication
 
Upvote 0
Try this:
Code:
Sub MyDeleteRows()

    Dim rng As Range, rngVisible As Range
    Dim rCell As Range, myArray() As Variant, i As Long
    Dim RowCt As Long
    
    Application.ScreenUpdating = False
    
'   Get range of data
    Set rng = Range("A1:C" & Cells(Rows.Count, "A").End(xlUp).Row)
    
'   See how many rows have "SEA" in column B and "C" in column C
    RowCt = Application.WorksheetFunction.CountIfs(Range("B:B"), "SEA", Range("C:C"), "C")
    
'   If RowCt>0 then delete rows with column A = 10252554
    If RowCt > 0 Then
'       Filter out records with "SEA" in column B and "C" in column C
        rng.AutoFilter Field:=2, Criteria1:="SEA"
        rng.AutoFilter Field:=3, Criteria1:="C"
'       Loop though visible rows storing codes from column A in array
        With rng
            Set rngVisible = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
                  .SpecialCells(xlCellTypeVisible)
        End With
        For Each rCell In rngVisible
            i = i + 1
            ReDim Preserve myArray(1 To i)
            myArray(i) = rCell
        Next rCell
'       Loop through array to build criteria
        For i = LBound(myArray) To UBound(myArray)
'           Filter data
            ActiveSheet.AutoFilter.ShowAllData
            rng.AutoFilter Field:=1, Criteria1:=myArray(i)
'           Delete visible data after filter
            Application.DisplayAlerts = False
            ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
            Application.DisplayAlerts = True
'           Remove filters
            ActiveSheet.AutoFilter.ShowAllData
            Next i
    End If
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Here is another option to try in a copy of your workbook.
Code:
Sub RemoveRows()
  Application.ScreenUpdating = False
  With Range("A1:C" & Range("A" & Rows.Count).End(xlUp).Row)
    Range("J2").Formula = Replace("=COUNTIFS(A$2:A$#,A2,B$2:B$#,""SEA"",C$2:C$#,""C"")", "#", .Rows.Count)
    .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("J1:J2"), Unique:=False
    .Offset(1).EntireRow.Delete
    If .Parent.FilterMode Then .Parent.ShowAllData
    Range("J2").ClearContents
  End With
  Application.ScreenUpdating = True
End Sub


If you happen to have a very large amount of data and both the suggested methods are too slow, post back with details as there would be a faster way (but longer code to write)
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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