Protecting Conditional Formatting

Written by Allen Wyatt (last updated June 17, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

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:

$B$3:$B$50,$D$3:$D$50,$G$3:$I$20

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 Not r Is Nothing Then
        If Not Application.Intersect(Target, r) Is Nothing Then
            SetCondFormat
        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, 2016, 2019, Excel in Microsoft 365, and 2021.

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. ...

MORE FROM ALLEN

Excel 2010 PivotTables for the Faint of Heart (Table of Contents)

PivotTables are a powerful tool for consolidating huge amounts of data. PivotTables 2010 for the Faint of Heart ...

Discover More

Quickly Entering Dates and Times

Excel provides keyboard shortcuts for a variety of purposes. This tip examines two such shortcuts, designed to allow ...

Discover More

Printing a Week of Planner Sheets

If you want to print multiple copies of a worksheet using a different footer for each copy, the easiest way is to rely on ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Conditionally Formatting Non-Integers

The conditional formatting capabilities of Excel are very helpful when you want to call attention to different values ...

Discover More

Shading Based on Odds and Evens

You can use conditional formatting to add shading to various cells in your worksheet. This tip shows how you can shade ...

Discover More

Finding Cells that Use Conditional Formatting

Conditional Formatting is a great boon to effectively displaying the information in your worksheets. If you want to ...

Discover More
Subscribe

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

View most recent newsletter.

Comments

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 7 - 2?

2023-06-17 12:22:07

J. Woolley

For related discussion, see:
https://excelribbon.tips.net/T006253_Copying_Conditional_Formatting.html
https://excelribbon.tips.net/T013021_Workbook_Loses_All_Conditional_Formatting.html
https://excelribbon.tips.net/T001143_Stopping_a_Conditional_Formatting_Rule_from_Breaking_into_Smaller_Ranges.html


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
Subscribe

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.