Protecting Conditional Formatting

by Allen Wyatt
(last updated September 6, 2021)

9

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 r IsNot Nothing Then
        If Application.Intersect(Target, r) IsNot 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, 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. ...

MORE FROM ALLEN

Indenting a Table

Insert a table into your document, and it normally appears aligned with the left margin. Word allows you to indent the ...

Discover More

Drawing a Curve

Ever wonder how to add a curved line to your document? With a little practice, adding curves is simple. Here's how.

Discover More

Changing Compression Print Resolution

The resolution at which Word compresses graphics in a document may be bothersome. If it is, your options are very ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Converting Conditional Formatting to Regular Formatting

Conditional formatting allows you to change how information is displayed based on rules you define. What if you want to ...

Discover More

Diagonal Borders in a Conditional Format

Conditional formatting is a great tool for changing how your data looks based on the data itself. Excel won't allow you ...

Discover More

Highlighting After-Hours Times

The Conditional Formatting capabilities of Excel are powerful. This tip shows how you can use a simple approach 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 2 + 4?

2022-03-25 10:03:44

J. Woolley

It looks like IsNot comes from VB.NET (aka .NET Visual Basic) and PowerShell. See https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/isnot-operator


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

Coenie

@J.Woolley

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

@Coenie
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 https://sites.google.com/view/MyExcelToolbox/


2022-03-24 02:12:33

Coenie

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

Coenie

'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: https://sites.google.com/view/MyExcelToolbox/


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.

Jerry,

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

Hi,

Checking, SetCondFormat doesn't seem to be an intrinsic function of Excel. I did find a thread on Stack Overflow at https://stackoverflow.com/questions/13661965/conditional-formatting-using-excel-vba-code/13664575 that provided the following code implementing SetCondFormat.

Sub setCondFormat()
Range("B3").Select
With Range("B3:H63")
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=IF($D3="""",FALSE,IF($F3>=$E3,TRUE,FALSE))"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
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
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.