Solved

Use VB to add multiple Numeric Conditional Formats to Pivot

Posted on 2011-09-30
1
773 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

760 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

22 Experts available now in Live!

Get 1:1 Help Now