Delete Rows based on Cells Color Criteria using VBA

srkrish

New Member
Joined
Jul 24, 2013
Messages
6
Hi Team,

Please help me with a solution for the below problem,

Need to retain a row and delete others for a given valid range, if atleast one cell in a row has filled with "Red" colour using VBA.

Sample is shown below,

Input:

1234
5(Fill Red)678
9101112
1314(Fill Red)1516
17181920

<TBODY>
</TBODY>










Output:

5(Fill Red)678
1314(Fill Red)1516

<TBODY>
</TBODY>





Regards,
Sathish
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
For example:

Code:
Sub autofilter_red()

    With Cells(1).CurrentRegion
        For i = 1 To .Columns.Count
            .AutoFilter i, vbRed, xlFilterCellColor
        Next
    End With


End Sub
 
Upvote 0
Hello

The previous code suggestion does not work, apologies.
This code copies the result to cell A1 on the second sheet of the file:

Code:
Sub autofilter_red()

    Dim rng As Range
    
    On Error Resume Next
    With Cells(1).CurrentRegion
        For i = 1 To .Columns.Count
            .AutoFilter
            .AutoFilter i, vbRed, xlFilterCellColor
            
            If Not rng Is Nothing Then
                Set rng = Application.Union(rng, .Columns(i).Offset(1).SpecialCells(xlCellTypeVisible))
            Else
                Set rng = .Columns(i).Offset(1).SpecialCells(xlCellTypeVisible)
            End If
            
        Next
        .AutoFilter
    End With


    If Not rng Is Nothing Then rng.EntireRow.Copy ThisWorkbook.Sheets(2).Cells(1)
        
End Sub
 
Upvote 0
Hi Wigi,

Thanks for the code. It works fine when there is only one cell in a row is filled with "Red", but if I have rows with more than one cell highlighted with "Red" then the code doesn't
function as expected. Please advise.

Regards,
Sathish
 
Upvote 0
With the help of 2 custom functions:

Code:
Sub autofilter_red()

    Dim rng As Range, rng_2 As Range


    On Error Resume Next
    With Cells(1).CurrentRegion
        For i = 1 To .Columns.Count
            .AutoFilter
            .AutoFilter i, vbRed, xlFilterCellColor
            Set rng = Union2(rng, .Columns(i).Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible))
        Next
        .AutoFilter
    End With
    On Error GoTo 0


    With ThisWorkbook.Sheets(2)
        If Not rng Is Nothing Then
            .Cells(1).Resize(, Cells(1).CurrentRegion.Columns.Count) = Cells(1).CurrentRegion.Rows(1).Value
            Set rng = ProperUnion(rng, Application.Intersect(rng.EntireRow, Cells(1).CurrentRegion))
            For Each rng_2 In rng.Areas
                rng_2.Copy .Cells(1).Offset(.UsedRange.Rows.Count)
            Next
        End If
    End With


End Sub


Function ProperUnion(ParamArray Ranges() As Variant) As Range
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ProperUnion
' This provides Union functionality without duplicating
' cells when ranges overlap. Requires the Union2 function.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim ResR As Range
    Dim N As Long
    Dim R As Range


    If Not Ranges(LBound(Ranges)) Is Nothing Then
        Set ResR = Ranges(LBound(Ranges))
    End If
    For N = LBound(Ranges) + 1 To UBound(Ranges)
        If Not Ranges(N) Is Nothing Then
            For Each R In Ranges(N).Cells
                If Application.Intersect(ResR, R) Is Nothing Then
                    Set ResR = Union2(ResR, R)
                End If
            Next R
        End If
    Next N
    Set ProperUnion = ResR
End Function


Function Union2(ParamArray Ranges() As Variant) As Range
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Union2
' A Union operation that accepts parameters that are Nothing.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim N As Long
    Dim RR As Range
    For N = LBound(Ranges) To UBound(Ranges)
        If IsObject(Ranges(N)) Then
            If Not Ranges(N) Is Nothing Then
                If TypeOf Ranges(N) Is Excel.Range Then
                    If Not RR Is Nothing Then
                        Set RR = Application.Union(RR, Ranges(N))
                    Else
                        Set RR = Ranges(N)
                    End If
                End If
            End If
        End If
    Next N
    Set Union2 = RR
End Function
 
Upvote 0

Forum statistics

Threads
1,216,045
Messages
6,128,486
Members
449,455
Latest member
jesski

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