Delete rows in a range where a particular cell color is not present

THEEND

New Member
Joined
Mar 17, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi,
I am still learning VBA and am trying to write some code that will delete all rows in the ActiveSheet.UsedRange where the cell color is not RGB(255, 255, 0), so the only remaining rows will those where this cell color appears.
Hope someone can advise - and many thanks in advance,
TE
 
To save row 1 don't start from I=1 but I = 2:
VBA Code:
For I = 2 To Cells(Rows.Count, TCol).End(xlUp).Row
If that resolves the problem then it'd be better to mark the discussion as Resoved; see the procedure: Mark as Solution
Many thanks - that worked perfectly - I have marked your last reply as "resolved solution".
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Apologies, I know this a few weeks old now, but I have been using the code for a while and wonder if there is a way that a message could pop up if there are no yellow cells found in the range.

Many thanks,
TE
 
Upvote 0
Try adding this "Else" condition at the end of the macro:
VBA Code:
If yCnt > 0 And Not R2K Is Nothing Then
    ActiveSheet.Copy after:=ActiveSheet     'AA-Original sheet backup copy
    Sheets(ActiveSheet.Index - 1).Select    'BB
    R2K.Delete
Else
    MsgBox ("No any Yellow cell in the worksheet")
End If
End Sub
 
Upvote 0
Hi - many thanks for that - this works if there are no yellow cells, however. if all cells in that column are yellow, the message still appears saying "No any Yellow cell in the worksheet". Here is the full code I am using:

VBA Code:
    Dim CCol As Long, TCol As String
    Dim I As Long, R2K As Range, yCnt As Long
    '
    TCol = "A"
    CCol = RGB(255, 255, 0)
    'CCol = Range("H1").Interior.Color   '<<< 2-ALTERNATE METHOD
    '
    For I = 2 To Cells(Rows.Count, TCol).End(xlUp).Row ' Change I = 1 to I = 2 to retain column headers
        If Cells(I, TCol).Interior.Color = CCol Then
            yCnt = yCnt + 1
        Else
            If R2K Is Nothing Then
                Set R2K = Rows(I)
            Else
                Set R2K = Application.Union(R2K, Rows(I))
            End If
        End If
    Next I
    If yCnt > 0 And Not R2K Is Nothing Then
        R2K.Delete
    Else
        MsgBox ("No any Yellow cell in the worksheet")
    End If
 
Upvote 0
Replace the Else with an ElseIf:
VBA Code:
ElseIf yCnt = 0 Then
    MsgBox ("No any Yellow cell in the worksheet")
End If
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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