Solved

Use VB to add multiple Numeric Conditional Formats to Pivot

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

806 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