Copying cells with Conditional Formatting Rules

Snake Eyes

Board Regular
Joined
Dec 14, 2010
Messages
121
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Greetings,
I have a range of cells that have working Conditional Formatting rules.
W374:X387

The formulas in the conditional format rules have absolute Column references and relative Row references.
An example of one: $W389


I want to copy these cells W374:X387 to W297:X310.

This second area W297:X310 is identical to the first and I want the contained formulas and conditional formats to work using the relative cell references for the 2nd area which would be $W312.

The formulas contained in the cells work, however, the Conditional Formats don't work until I make the cell references in the rules absolute for the Column AND the Row ($W$312).

The fix seems to be to use the absolute cell reference ($W$389) BEFORE I copy the groups of cells to the new area W297:X310.

When I do this and then copy the section over, the Conditional Format rule now references the original cell ($W$389) and not the relative cell it needs to ($W312).

How is one supposed to work around this?

BTW, once I figure this out, this copy process will need to work for 19 areas in the same worksheet so, I'm trying to avoid manually editing several conditional formatting rules 19 times.
 
I'm not clear what you've got here ...

It sounds like for every cell in W297:X310, conditional formatting should refer to $W$312? This can be set up by selecting the whole range W297:X310, and applying one CF rule.

Which presumably means that similar should apply in W374:X387, with every cell referring to $W$389? If so, again, only one rule is needed.

But it sounds like you are using relative references $W389, and every line uses this relative reference $W389? This would imply you have set up at least 14 CF rules for this selection? But if this was the case, each relative reference should update correctly to $W312 when copied to the new range.

The gist of your post, I think, is that you have a block of conditional formatting all pointing to a particular row (absolute) adjacent to the block. You want to copy that block to another block and have that row reference adjust relative to the new block.

Depending on the number of copies, I'd consider using VBA to change the CF formulae. But if you're manually copying and pasting only 19 times, it wouldn't take much extra time to change the row number in the CF rule each time. But before doing this, I'd make sure there was only one CF rule in the original block to be copied, so that only 19 changes are required.
 
Upvote 0
Maybe if I include a couple of screenshots this may clarify my concern.
I have a block of cells that have working Conditional Formatting rules.
W374:X387

The formulas in the conditional format rules have absolute Column references and relative Row references.
An example of one: $W389
1742494775954.png


I want to copy this block W374:X387 to W297:X310.
This second block W297:X310 is identical to the first and I want the contained formulas and conditional formats to work using the relative cell references for the 2nd block which would be $W312.
1742494877313.png


The formulas contained in the cells work, however, the Conditional Formats don't work until I make the cell references in the rules absolute for the Column AND the Row ($W$312).
1742495063781.png



The fix seems to be to use the absolute cell reference ($W$389) BEFORE I copy the block of cells to the new block W297:X310.

When I do this and then copy the block over, the Conditional Format rule now references the original cell ($W$389) and not the relative cell it needs to ($W312).

How is one supposed to work around this?
 

Attachments

  • 1742494625402.png
    1742494625402.png
    19 KB · Views: 1
  • 1742494695711.png
    1742494695711.png
    25.5 KB · Views: 2
Upvote 0
If you have a CF formula in W374:X387 referring to $W389, and you copy W374:X387 to W297:X310 then the CF formula there will refer to $W312, based on the relative reference.

So if the CF in W374:X387 is working correctly, and you want it to work identically in W297:X310, the copy should work.

But I think you might be misunderstanding what you're starting with in W374:X387. The CF formula shown is the formula for the top left cell W374. The relative row reference means that:

W374:X374 refer to $W389
W375:X375 refer to $W390
W376:X376 refer to $W391
etc

Here's a simple example to illustrate:

ABC
1
2A1
3B2
4C3
5D4
6E5
7
8Hello
9
10Hello
11Hello
12
13
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B2:C6Expression=$C8="Hello"textNO

1742508933643.png

Whereas I think you wanted something more like this?

ABC
1
2A1
3B2
4C3
5D4
6E5
7
8Hello
9
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B2:C6Expression=$C$8="Hello"textNO


Here's an example of how you can use VBA to copy CF blocks, updating absolute references to make them relative to the new block(s).

Before:

ABC
1
2A1
3B2
4C3
5D4
6E5
7
8Yes
9
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B2:C6Expression=$C$8="No"textYES
B2:C6Expression=$C$8="Maybe"textYES
B2:C6Expression=$C$8="Yes"textYES

VBA Code:
Sub CopyCF()

    Dim rng1 As Range, rng2 As Range, r As Range, i As Long, j As Long
    Dim step As Long
    Dim f As FormatCondition
    
    Set rng1 = Range("B2:C6")
    i = rng1.Rows.Count
    j = rng1.Columns.Count
    Set rng2 = Range("D13,C22")  'arbitrary copy destinations - first cell
    step = 2
    
    For Each r In rng2.Areas
        Set rng2 = r.Resize(rng1.Rows.Count, rng1.Columns.Count)
        rng1.Copy rng2
        For Each f In rng2.FormatConditions
            f.Modify xlExpression, , Replace(f.Formula1, "$C$8", rng2(i + step, j).Address)
        Next f
    Next r
    
    'hardcoded - to test this particular scenario
    Range("E19").Value = "Maybe"
    Range("D28").Value = "No"
        
End Sub

After:
ABCDEF
1
2A1
3B2
4C3
5D4
6E5
7
8Yes
9
10
11
12
13A1
14B2
15C3
16D4
17E5
18
19Maybe
20
21
22A1
23B2
24C3
25D4
26E5
27
28No
29
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B2:C6Expression=$C$8="No"textYES
B2:C6Expression=$C$8="Maybe"textYES
B2:C6Expression=$C$8="Yes"textYES
D13:E17Expression=$E$19="No"textYES
D13:E17Expression=$E$19="Maybe"textYES
D13:E17Expression=$E$19="Yes"textYES
C22:D26Expression=$D$28="No"textYES
C22:D26Expression=$D$28="Maybe"textYES
C22:D26Expression=$D$28="Yes"textYES
 
Last edited:
Upvote 0
Thank you for the detailed explanation, examples, and VB Code StephenCrump.
I'll study this and work towards implementing the VB Code into my worksheet.

If I understand this, copying one block to another block and have the CF work correctly, absolute cell references but must be utilized ($W$389) along with the VB code to allow those absolute references to become relative to the new block once copied. Is this correct?
 
Upvote 0
If I understand this, copying one block to another block and have the CF work correctly, absolute cell references but must be utilized ($W$389) along with the VB code to allow those absolute references to become relative to the new block once copied. Is this correct?
I wouldn't say it this way. But I'm still not clear on what CF rules you have, and how they should be copied.

I am guessing that your setup is analogous to mine in Post #4, where you have a block of cells, with CF all referring to the same row. This is best set up as one CF rule, referring to an absolute row number.

The problem if you want to copy this CF to a new block is that you need to update that row reference. It could be done manually. But if there are too many to make this practical, VBA is one way you could automate the change. You could also look at having a helper column to reference in the CF formula to point you to the right row.

I am not aware of any smarter way that you can do this directly, rather than indirectly.
 
Upvote 0

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