Solved

Conditional Formatting not copying properly.

Posted on 2014-01-17
12
541 Views
Last Modified: 2014-01-17
I have a conditional format on a cell that I would like to copy down the whole sheet.
    Range("G3").FormatConditions.Add Type:=xlExpression, Formula1:="=IF(G3=""PASSED"",TRUE,FALSE)"
    Range("G3").FormatConditions(Range("G3").FormatConditions.Count).SetFirstPriority
    Range("G3").FormatConditions(1).Font.Color = -11489280
    Range("G3").FormatConditions(1).Font.TintAndShade = 0
    Range("G3").FormatConditions(1).StopIfTrue = False

Open in new window


The problem is when I copy it down using the format painter, all the cells will reference the first cell in the range I'm formatting so in this case they are all looking at G4 instead of their respective rows.
I could do this with a loop but I'd rather not if possible. It takes too long to run.
Any idea?
0
Comment
Question by:stopher2475
12 Comments
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39788642
Since your formula is just checking for equality to a specific value you can use cell value equals instead of an expression:
.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""PASSED"""

Open in new window


(I think your formula isn't working because the G3 is hardcoded into the formula and apparently the format painter isn't changing it on paste - this dodges that issue)
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39788734
Try using r1c1 (relative addressing) in your .Add method.
Formula1:="=IF(RC=""PASSED"",TRUE,FALSE)"

Open in new window

0
 
LVL 2

Author Comment

by:stopher2475
ID: 39788835
I actually want to leave it as G3 becuase I'm going to format the 2 cells to the left of it based on that cells value. Will try the RC notation. I think aikimark is onto something.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39788850
Attacking the problem from the other direction, what code are you using for the format painter?

I just recorded a macro and it gave me this (and seemed to work):
    Selection.Copy
    Range("C7:C8").Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False

Open in new window


Also, if you're going to use the same formula on another column you probably want a $ infront of your G to make it $G3.
0
 
LVL 2

Author Comment

by:stopher2475
ID: 39788861
Tried the RC notation for the range.
Range("G3:G2310").FormatConditions.Add Type:=xlExpression, Formula1:="=IF(RC=""PASSED"",TRUE,FALSE)"
    Range("G3:G2310").FormatConditions(Range("G3:G2310").FormatConditions.Count).SetFirstPriority
    Range("G3:G2310").FormatConditions(1).Font.Color = -11489280
    Range("G3:G2310").FormatConditions(1).Font.TintAndShade = 0
    Range("G3:G2310").FormatConditions(1).StopIfTrue = False]

Open in new window

The expression in all of them is referencing the first cell of the range.
    =IF(G3="PASSED",TRUE,FALSE)
How can I get it to reference the rows validation cell?
0
 
LVL 30

Expert Comment

by:hnasr
ID: 39788964
Try:
   
    Range("G3").FormatConditions.Add Type:=xlExpression, Formula1:="=IF(G3=""PASSED"",TRUE,FALSE)"
    Range("G3").FormatConditions(Range("G3").FormatConditions.Count).SetFirstPriority
    Range("G3").FormatConditions(1).Font.Color = -11489280
    Range("G3").FormatConditions(1).Font.TintAndShade = 0
    Range("G3").FormatConditions(1).StopIfTrue = False
    Range("G3").Copy
    Range("G4:G20").Select
    Range("G4").PasteSpecial xlPasteAllMergingConditionalFormats

Open in new window

0
 
LVL 2

Author Comment

by:stopher2475
ID: 39789002
That only does the one cell G4. I change line 8 to:
Range("G4:G20").PasteSpecial xlPasteAllMergingConditionalFormats
but they all reference G4 for the check.
I think I may be stuck with using a loop. Sucks because it's a lot slower.
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39789157
stopher, I think you missed my last comment.

    Selection.Copy
    Range("C7:C8").Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False

Open in new window


Produced this paste formats code with record macro and this worked for me on a small scale test.
0
 
LVL 11

Accepted Solution

by:
Angelp1ay earned 500 total points
ID: 39789184
See attached example workbook. Run the macro "demo". Works perfectly for me.

Sub Demo()

    Dim template As Range, destination As Range
    Set template = Range("G3")
    Set destination = Range("G4:G20")
    
    template.FormatConditions.Add Type:=xlExpression, Formula1:="=IF($G3=""PASSED"",TRUE,FALSE)"
    template.FormatConditions(template.FormatConditions.Count).SetFirstPriority
    With template.FormatConditions(1)
        .Font.Color = -11489280
        .Font.TintAndShade = 0
        .StopIfTrue = False
    End With

    template.Copy
    destination.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

End Sub

Open in new window


Note - the conditional formatting appears as 2 rules; one covering G3 and one covering G4:G20. If you want to be fancier you should probably make the template an arbitrary cell outside the destination range, paste to the whole of G3:G20, and then wipe the template.
ConditionalFormatPaste.xlsm
0
 
LVL 2

Author Comment

by:stopher2475
ID: 39789379
What's weird is that works but when I check the format it appears to be referencing the first Cell "G4". It was probably working before but when I saw the reference to the first cell I had assumed it wasn't. I guess I don't understand the notation fully.
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39789400
In the conditional formatting manager the formula shown is always that for the first cell in the range it's applied to. We probably should have realised that was what you were looking at :/
0
 
LVL 2

Author Comment

by:stopher2475
ID: 39790193
Good to know. Thanks for the help!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question