Solved

Use VB to add multiple Numeric Conditional Formats to Pivot

Posted on 2011-09-30
1
777 Views
Last Modified: 2012-06-27
I am working on an Excel 2010 PowerPivot table and need to apply different numeric formatting depending on the value of another cell.  Each measure in the pivot will have 3 possible conditional formats.  If I record the macros to accomplish this, it includes a line like: ExecuteExcel4Macro "(2,1,""0.0%"")"
This line will not execute when re-running the macro.  So I have modified the macro so that it does run, but it does not process properly.   I get the three conditions, but the second and third have no format set.  I feel like I am really close, but have not been able to make any headway on this.  


Sub CondFormat()
'

'Condition 1 Percent
    Selection.NumberFormat = "0.0%"
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=$K$31=""Pct"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(Selection.FormatConditions.Count).NumberFormat _
    = "0.0%"
    'ExecuteExcel4Macro "(2,1,""0.0%"")"
    
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions(1).ScopeType = xlDataFieldScope  'xlDataField Scope for Measure; xlFieldsScope for Filtered Measure
'Condition 2 Points
    Selection.NumberFormat = "#,##0.0"
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=$I$31=""Pts"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(Selection.FormatConditions.Count).NumberFormat _
    = "#,##0.0"
   ' ExecuteExcel4Macro "(2,1,""#,##0.0_);(#,##0.0)"")"
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions(1).ScopeType = xlDataFieldScope
'Condition 3 Numeric
    Selection.NumberFormat = "#,##0_);(#,##0)"
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=$I$31=""Num"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(Selection.FormatConditions.Count).NumberFormat _
    = "#,##0_);(#,##0)"
'    ExecuteExcel4Macro "(2,1,""#,##0_);(#,##0)"")"
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions(1).ScopeType = xlDataFieldScope

End Sub

Open in new window

CondFormat.PNG
0
Comment
Question by:ImageryGrl
1 Comment
 

Accepted Solution

by:
ImageryGrl earned 0 total points
ID: 36894992
I found the answer!  The key was that I had to explicitly set the priority of each rule.  

im r As Integer, c As Integer, t As Integer
    r = 43  'Row 43 is the first row of the data in the Pivot
    c = 9   ' Colummn 9 "I" is the firt column in the Breathable area

With ActiveSheet
    For c = 9 To 49  'Cycle thru the columns in the pivot table
   
     If .Cells(r - 1, c).Value = "" Then Exit For
    .Cells(r, c).Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=R31C" & c & "=""Pts"""
    Selection.FormatConditions(Selection.FormatConditions.Count).Priority = 1
    Selection.FormatConditions(Selection.FormatConditions.Count).NumberFormat = "#,##0.0"
    Selection.FormatConditions(1).ScopeType = xlDataFieldScope
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=R31C" & c & "=""Pct"""
    Selection.FormatConditions(Selection.FormatConditions.Count).Priority = 2
    Selection.FormatConditions(Selection.FormatConditions.Count).NumberFormat = "0.0%"
    Selection.FormatConditions(2).ScopeType = xlDataFieldScope
    Selection.FormatConditions(2).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=R31C" & c & "=""Num"""
    Selection.FormatConditions(Selection.FormatConditions.Count).Priority = 3
    Selection.FormatConditions(Selection.FormatConditions.Count).NumberFormat = "#,##0_);(#,##0)"
    Selection.FormatConditions(3).ScopeType = xlDataFieldScope
    Selection.FormatConditions(3).StopIfTrue = False
   
   Next c
End With
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

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

895 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

18 Experts available now in Live!

Get 1:1 Help Now