Home » excel » vba – Excel: How to copy/paste Conditional Formatting, as if the condition were a "value"?

vba – Excel: How to copy/paste Conditional Formatting, as if the condition were a "value"?

Posted by: admin May 14, 2020 Leave a comment

Questions:

Just like Excel allows you to Copy & Paste cells as a Value (ignoring formatting), how can I do the opposite?

In this example, I want to retain the yellow Conditional Formats, even after I delete Column R. So in a way retain the condition as a “value” inside the cells in Column Q.

enter image description here

Currently I have to hide Column R so the conditional formatting is retained. If I remove the column entirely, I’ve then lost the formatting because it’s based on a formula.

i.e.

=IF(R2="*",true,false)

I wish Excel had a Copy/Paste Colours.

How to&Answers:

As Brad Dixon said, VBA was the only solution…

Public Sub HighlightColumn()
    Dim tbl As ListObject
    Dim wsCell As Range
    Dim rw As Range

    'loop through each row
    Set tbl = ActiveSheet.ListObjects("Table_1")  'my data is inside a table
    For Each rw In tbl.Range.SpecialCells(xlCellTypeVisible).Rows
        Set wsCell = rw.Cells(1, 17)
        If rw.Cells(1, 18) = "*" Then
            With wsCell
                .Interior.Color = vbYellow
            End With
        End If
    Next rw

    'the golden moment!
    tbl.Range(, 18).EntireColumn.Delete
End Sub