Pivot Table Creation with Optional Arguments
Hi,
I need support in creating pivot with a code base, below is the code that i am using to create pivot. It works perfectly as expected for one parameter for row field or column field etc. But now i need to create a pivot with multiple row fields and column fields and data fields.
If anyone can support on how to pass arguments for multiple values for one parameter, it would be of great help
Thanks
Raj :)
Code:
Sub MakePivotCriteria(SourceSheetName As String, SourceTableName As String, RowField As String, ColField As String, DataField As String, PageField As String)
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SourceTableName).CreatePivotTable _
TableDestination:=Worksheets(SourceSheetName).Range("A1"), TableName:=SourceTableName
With ActiveSheet.PivotTables(SourceTableName).PivotFields(ColField)
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables(SourceTableName).PivotFields(RowField)
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables(SourceTableName).PivotFields(DataField)
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "0"
End With
With ActiveSheet.PivotTables(SourceTableName).PivotFields(PageField)
.Orientation = xlPageField
.Position = 1
.EnableMultiplePageItems = True
End With
With Worksheets(SourceSheetName).PivotTables(SourceTableName)
.TableStyle2 = "PivotStyleDark5"
End With
End Sub
Pivot Creation by Optional Arguments
Hi Aflatoon / Paul,
Many thanks, for your reply. Works perfect as needed. I needed one more small change as below, will it be possible to accommodate the same.
Code:
Sub MakePivotCriteriaCount(SourceSheetName As String, SourceTableName As String, RowField As String, _
ColField As String, DataField As String, PageField As String, ArithOps As Boolean)
With ActiveSheet.PivotTables(SourceTableName).PivotFields(DataField)
.Orientation = xlDataField
.Position = 1
If ArithOps = True Then
.Function = xlCount
.Function = xlSum
End If
.NumberFormat = "0"
End With
Raj
Pivot Table Creation by Optional Arguments
Works Perfect!! Thanks a Ton
I could reduce add-in file size by 2mb, thanks again
Raj