Need Help with VBA removing conditional formatting

Rick1006

New Member
Joined
Nov 2, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello -

Sorry if this has been posted before, however, I didn't see anything for this type of issue.

I am trying to remove the conditional formatting while leaving the color coding. I used the following vba code prior and it worked - now it's removing the formatting but turning the cell colors black. They should remain either blank, red or green depending on cell value. Any help would be appreciated

Sub removeConditionalFormattingButKeepColors()
Dim cell As Range

For Each cell In Selection
cell.Interior.Color = cell.DisplayFormat.Interior.Color
Next

Selection.FormatConditions.Delete

MsgBox "Done removing conditional formatting rules."

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
See if this works for you:

VBA Code:
Sub Remove_CF_Keep_Format()
'
    Dim Cel As Range
    Dim Rng As Range
    Dim xTxt As String
'
    On Error Resume Next
'
    If ActiveWindow.RangeSelection.Count > 1 Then
        xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
        xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
'
    Set Rng = Application.InputBox("Select range:", "Please enter desired Range", xTxt, , , , , 8)
'
    If Rng Is Nothing Then Exit Sub
'
    For Each Cel In Rng
        With Cel
            .Font.FontStyle = .DisplayFormat.Font.FontStyle
            .Font.Strikethrough = .DisplayFormat.Font.Strikethrough
            .Interior.Pattern = .DisplayFormat.Interior.Pattern
'
            If .Interior.Pattern <> xlNone Then
                .Interior.PatternColorIndex = .DisplayFormat.Interior.PatternColorIndex
                .Interior.Color = .DisplayFormat.Interior.Color
            End If
'
            .Interior.TintAndShade = .DisplayFormat.Interior.TintAndShade
            .Interior.PatternTintAndShade = .DisplayFormat.Interior.PatternTintAndShade
        End With
    Next
'
    Rng.FormatConditions.Delete
End Sub
 
Upvote 0
Thanks, but that too changed the cells to black. See attached image. Could this be due to the type of XLS workbook I am using? Should it be a macro enabled WB?
 

Attachments

  • Screenshot_VBA.png
    Screenshot_VBA.png
    4.1 KB · Views: 10
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,760
Members
449,095
Latest member
m_smith_solihull

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