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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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,543
Messages
6,125,423
Members
449,223
Latest member
Narrian

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