Solved

Conditional Formatting not copying properly.

Posted on 2014-01-17
12
529 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
 
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

914 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now