Protecting Conditional Formatting

Written by Allen Wyatt (last updated May 26, 2022)
This tip applies to Excel 2007, 2010, 2013, and 2016


Kees has a worksheet that uses conditional formatting extensively. However, the conditional formatting keeps getting messed up when users copy and paste information or when they use drag and drop to edit the worksheet. He wonders about the best way to prevent this from happening.

This happens because conditional formatting is considered just that—formatting. A standard copy and paste (or a drag and drop edit) copies everything, including formatting. This means the target cells will have the formatting of the source cells, not the formatting of the target cells (including any conditional formatting that may have been in the target cells).

The answer, then, is to tell users not to do a standard copy and paste. Instead, they should use the paste options to paste anything (or everything) except formatting.

Another option, of course, is to protect the worksheet so that the user cannot copy and paste anything. This may be a bit drastic for your users, however, as you may want them to make changes. (You just don't want them to mess up the conditional formatting.)

This leads to a macro approach. If you can record a macro that applies the conditional formatting to the cells, you could create some additional macros that apply that recorded macro, as needed. For instance, let's say that the macro you record is called something short and sexy like SetCondFormat.

Next, go into the Visual Basic Editor and, in the Immediate window, enter the following:

? Cells.SpecialCells(xlCellTypeAllFormatConditions).Address

Assuming you have your conditional formatting all set up, this should return a line—perhaps a long line—that shows the addresses of the cells and ranges that use conditional formatting. It will look something like this:


Next, add the following macro to the ThisWorksheet code module:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r As Range

    Const cCFAddress = "$B$3:$B$50,$D$3:$D$50,$G$3:$I$20"

    On Error Resume Next
    Set r = Range(cCFAddress)
    On Error GoTo 0

    If r IsNot Nothing Then
        If Application.Intersect(Target, r) IsNot Nothing Then
        End If
    End If
End Sub

The key here is to make sure that the cCFAddress constant is set equal to whatever was returned when you saw the addresses in the Immediate window. (If you changing conditional formatting at a later time, you can use the Immediate window trick again and simply change the line in the above macro.)

The macro is executed every time there is a change in the worksheet. It checks to see if the changed address (passed in the Target variable) is part of the original cells that contained conditional formats. If so, then your SetCondFormat (the one you recorded to do the conditional formatting) is again executed.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (4362) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...


Replacing Letters with Numbers

If you have a text string that contains both letters and numbers and you want to convert those letters to numbers ...

Discover More

ExcelTips: The Macros (Special Offer)

ExcelTips: The Macros is the definitive reference on how to use macros in Excel. You can stay at the top of your ...

Discover More

Printing the Active Document from a Macro

When you process a document in a macro, you may also want to print that document from within the same macro. Here's how ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Conditional Formats that Distinguish Blanks and Zeroes

Conditional formatting is a great tool. You may need to use this tool to tell the difference between cells that are empty ...

Discover More

Conditionally Formatting Cells Containing Dates

Need to know if a particular cell contains a date value? Excel doesn't have a worksheet function to determine this ...

Discover More

Conditional Format that Checks for Data Type

Conditional formatting can be used to highlight cells that contain the improper type of data for your needs. This tip ...

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 0?

2022-05-27 09:55:29

J. Woolley


2022-05-26 11:48:31


I would love to use this tip, as I have created A TON of Conditional Formatting Rules for my worksheet that always seemed to get messed up. I don't have the time or the need to become an Excel expert (even though I read these tips everyday), but it seems like it would be better to post screen shots from Excel and VBA of where all this code is going, instead of just the code itself. I understand how to record a Macro, but one that "that applies the conditional formatting to the cells" is beyond me. Thanks for all the tips!

2022-03-25 10:03:44

J. Woolley

It looks like IsNot comes from VB.NET (aka .NET Visual Basic) and PowerShell. See

2022-03-25 06:15:52

Willy Vanhaelen

What a dumb mistake! IsNot is no keyword in vba.

The correct syntax is:

If Not r is Nothing Then
If Not Application.Intersect(Target, r) Is Nothing Then

This proves that the macro in this tip has not been tested before publication!

2022-03-24 22:44:43



Thanks I'll give it a go and see if I can get it to work. One thing I have noticed is that I get a compiling error at "IsNot"

(see Figure 1 below)

Figure 1. Compiling Error

2022-03-24 12:40:23

J. Woolley

If you have VBA code for two Worksheet_Change events, separate them into two Sub procedures like this:

Private Sub Worksheet_Change(ByVal Target As Range)
    Proc1 Target
    Proc2 Target
End Sub

In the Visual Basic Editor (VBE), use Insert > Module to add a standard code module, then add the two Sub procedures to that module like this:

Sub Proc1(ByVal Target As Range)
    ... code from 1st Worksheet_Change event
End Sub

Sub Proc2(ByVal Target As Range)
    ... code from 2nd Worksheet_Change event
End Sub

Also, see

2022-03-24 02:12:33


Hi Allen,

I have tested your solution and it works for me. However, I'm not a vba expert but I have another worksheet change event already, how can I combine the two? Any help would be much appreciated

Kind Regards


'Private Sub Worksheet_Change(ByVal Target As Range)
' Dim OldVal As String
' Dim NewVal As String
' ' If more than 1 cell is being changed
' If Target.Count > 1 Then Exit Sub
' If Not Intersect(Target, ActiveSheet.Range("A5:A5000,P5:P5000,V5:V5000")) Is Nothing Then
' Application.EnableEvents = False
' NewVal = Target.Value
' 'If there's nothing to undo this will cause an error
' On Error Resume Next
' Application.Undo
' On Error GoTo 0
' OldVal = Target.Value
' If OldVal = "" Then
' Target.Value = NewVal
' Else
' 'Delete cell contents
' If NewVal = "" Then
' Target.Value = ""
' Else
' 'This IF prevents the same value apprearing in the cell multiple times
' 'If you are happy to have the same value multiple timesremove this IF
' If InStr(Target.Value, NewVal) = 0 Then
' Target.Value = OldVal & "; " & NewVal
' End If
' End If
' End If
' Application.EnableEvents = True
' Else
' Exit Sub

2020-10-07 10:06:18

J. Woolley

You might also be interested in the conditional formatting backup and restore macros at My Excel Toolbox:

2020-10-06 03:06:29

Sytze Visser

For those who wonder, this will not work on an Excel spreadsheet hosted as an Office365 online document. The VBA code will be ignored.

2017-10-03 13:54:38

Chris G.


From the article: "If you can record a macro that applies the conditional formatting to the cells, you could create some additional macros that apply that recorded macro, as needed. For instance, let's say that the macro you record is called something short and sexy like SetCondFormat."

2017-10-02 17:57:46

Jerry Herman


Checking, SetCondFormat doesn't seem to be an intrinsic function of Excel. I did find a thread on Stack Overflow at that provided the following code implementing SetCondFormat.

Sub setCondFormat()
With Range("B3:H63")
.FormatConditions.Add Type:=xlExpression, Formula1:= _
With .FormatConditions(.FormatConditions.Count)
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 5287936
.TintAndShade = 0
End With
End With
End With
End Sub

It appears that the full solution would a user to identify and code for the formatting properties that are desired.

This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.