Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Use VB to add multiple Numeric Conditional Formats to Pivot

Posted on 2011-09-30
1
Medium Priority
?
835 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

715 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