Guest User

Toolkit Add-In for Excel (Pivotfield formatting)

a guest
Apr 5th, 2014
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Sub setPivotFieldType(iFieldType As Integer)
  4. ' sets the Field Type (sum, average, etc) in a selected range of pivotfields
  5.    ' sanity test
  6.    If TypeName(Selection) <> "Range" Then Exit Sub
  7.     toggleStatus
  8.     On Error Resume Next
  9.     ' test for end of used fields; keeps from running through an entire column or row
  10.    Dim c As Range, ptField As PivotField, intEndRow As Integer, intEndCol As Integer
  11.     intEndRow = Selection.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  12.     intEndCol = Selection.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
  13.    
  14.     ' for each selected cell, get the pivotfield and set it to the field type
  15.    For Each c In Selection
  16.         Set ptField = c.PivotField
  17.         If Not ptField Is Nothing Then _
  18.             ptField.Function = iFieldType
  19.         If c.Row > intEndRow Or c.Column > intEndCol Then Exit Sub
  20.     Next c
  21.     toggleStatus True
  22. End Sub
  23.  
  24. Sub setPivotFieldFormat(strFormat As String)
  25. ' sets the format of a field (or cell if not a pivot table) according to the strFormat (in NumberFormat style)
  26.    ' sanity test
  27.    If TypeName(Selection) <> "Range" Then Exit Sub
  28.     toggleStatus
  29.     On Error Resume Next
  30.     ' test for end of used fields; keeps from running through an entire column or row
  31.    Dim c As Range, ptField As PivotField, intEndRow As Integer, intEndCol As Integer
  32.     intEndRow = Selection.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  33.     intEndCol = Selection.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
  34.    
  35.     ' for each selected cell, get the pivotfield and set it to the field type
  36.    For Each c In Selection
  37.         Set ptField = c.PivotField
  38.         If ptField Is Nothing Then
  39.          Selection.NumberFormat = strFormat
  40.         Else
  41.          ptField.NumberFormat = strFormat
  42.         End If
  43.         If c.Row > intEndRow Or c.Column > intEndCol Then Exit Sub
  44.     Next c
  45.     toggleStatus True
  46. End Sub
  47.  
  48. ' FieldType buttons
  49. Sub setPivotFieldTypeSum(Optional control As IRibbonControl)
  50.     setPivotFieldType (xlSum)
  51. End Sub
  52. Sub setPivotFieldTypeAvg(Optional control As IRibbonControl)
  53.     setPivotFieldType (xlAverage)
  54. End Sub
  55. Sub setPivotFieldTypeCnt(Optional control As IRibbonControl)
  56.     setPivotFieldType (xlCount)
  57. End Sub
  58. Sub setPivotFieldTypeVar(Optional control As IRibbonControl)
  59.     setPivotFieldType (xlVar)
  60. End Sub
  61. Sub setPivotFieldTypeStDev(Optional control As IRibbonControl)
  62.     setPivotFieldType (xlStDev)
  63. End Sub
  64. Sub setPivotFieldTypeMax(Optional control As IRibbonControl)
  65.     setPivotFieldType (xlMax)
  66. End Sub
  67. Sub setPivotFieldTypeMin(Optional control As IRibbonControl)
  68.     setPivotFieldType (xlMin)
  69. End Sub
  70.  
  71. ' Format buttons
  72. Sub setPivotFieldFormatGen(Optional control As IRibbonControl)
  73.     setPivotFieldFormat ("0")
  74. End Sub
  75. Sub setPivotFieldFormatNum(Optional control As IRibbonControl)
  76.     setPivotFieldFormat ("#,###")
  77. End Sub
  78. Sub setPivotFieldFormatAct(Optional control As IRibbonControl)
  79.     setPivotFieldFormat ("_(* #,##0.0_);_(* (#,##0.0);_(* "" - ""?_);_(@_)")
  80. End Sub
  81. Sub setPivotFieldFormatPct(Optional control As IRibbonControl)
  82.     setPivotFieldFormat ("0.0%")
  83. End Sub
  84. Sub setPivotFieldFormatCur(Optional control As IRibbonControl)
  85.     setPivotFieldFormat ("$0.00;-$0.00")
  86. End Sub
  87. Sub setPivotFieldFormatSci(Optional control As IRibbonControl)
  88.     setPivotFieldFormat ("0000E+00")
  89. End Sub
  90. Sub setPivotFieldFormatBig(Optional control As IRibbonControl)
  91.     setPivotFieldFormat ("0, k")
  92. End Sub
  93. Sub setPivotFieldFormatWho(Optional control As IRibbonControl)
  94.     setPivotFieldFormat ("#,##0")
  95. End Sub
  96. Sub setPivotFieldFormatCustom(Optional control As IRibbonControl)
  97.     If TypeName(Selection) <> "Range" Then Exit Sub
  98.     frm_FieldFormat.Show
  99. End Sub
Advertisement
Add Comment
Please, Sign In to add comment