Delete filled column based on font color

Nancy123

New Member
Joined
Jan 15, 2021
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
Hi Everyone,
Kindly need some solution regarding my project here.
Here is my VBA code, what I want to do is :
1. If the cells in column A have duplicate values, then I will change the font color from black to red for the duplicate values starting from the second occurance (DONE for the code)
2. For rows which have red font color in column A, I want column E until H in that row to be blank (NEED HELP)
3. Then I will delete the conditional formatting so that all cells in column A will have black font color again (DONE for the code)

VBA Code:
Sub SecondOcurrenceFont()
    
'Give red color to Second Ocurrence

Sheet3.Activate

Columns("A:A").FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=COUNTIF($A$1:A1,A1)>1"
        
With Columns("A:A").FormatConditions(1).Font
    .Color = -16776961
End With
    
'For red color in A give blank to E until H
[B][COLOR=rgb(184, 49, 47)]If Columns("A:A").Font.Color = -16776961 Then[/COLOR][/B]
[COLOR=rgb(184, 49, 47)][B]
    Else
    
    [/B][/COLOR]
[B][COLOR=rgb(184, 49, 47)]    End If[/COLOR][/B]
    

'Delete Condition

Columns("A:A").FormatConditions.Delete
    
    
End Sub
I've attached the excel display as well here.

Thanks a lot for your help.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Sorry, the code is actually like this, I haven't done for the part "For red color in A give blank to E until H"

VBA Code:
Sub SecondOcurrenceFont()
    
'Give red color to Second Ocurrence

Sheet3.Activate

Columns("A:A").FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=COUNTIF($A$1:A1,A1)>1"
        
With Columns("A:A").FormatConditions(1).Font
    .Color = -16776961
End With
    
'For red color in A give blank to E until H
If Columns("A:A").Font.Color = -16776961 Then

    Else
    
    
    End If
    

'Delete Condition

Columns("A:A").FormatConditions.Delete
    
    
End Sub
 
Upvote 0
How about
VBA Code:
Sub SecondOcurrenceFont()
    
'Give red color to Second Ocurrence

Sheet3.Activate
With Range("A1", Range("A" & Rows.Count).End(xlUp))
   .FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=COUNTIF($A$1:A1,A1)>1"
        
   .FormatConditions(1).Font.Color = -16776961
   .AutoFilter 1, RGB(255, 0, 0), xlFilterFontColor
   .Parent.AutoFilter.Range.Offset(1, 4).Resize(, 4).Value = ""
   .FormatConditions.Delete
End With
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub SecondOcurrenceFont()
   
'Give red color to Second Ocurrence

Sheet3.Activate
With Range("A1", Range("A" & Rows.Count).End(xlUp))
   .FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=COUNTIF($A$1:A1,A1)>1"
       
   .FormatConditions(1).Font.Color = -16776961
   .AutoFilter 1, RGB(255, 0, 0), xlFilterFontColor
   .Parent.AutoFilter.Range.Offset(1, 4).Resize(, 4).Value = ""
   .FormatConditions.Delete
End With
End Sub
Hi @Fluff,

Thanks for your help, it works. However, it also filtered the blank cells in column A, can we get a formula to just filter the red color cells without any blank cells ?

And also how can I clear / unfilter the data (Show all data back) ?

Thank you.
 
Upvote 0
Does col A have formulae that return ""
 
Upvote 0
As the countif does not affect empty cells, your blanks are not actually empty. Where did the data come from?
 
Upvote 0

Forum statistics

Threads
1,215,618
Messages
6,125,870
Members
449,266
Latest member
davinroach

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