There is a budget spreadsheet mentioned below:
VBA code for creating pivot table with variance analysis:
Sub Macro1()
'' Macro1 Macro
'
'
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _Range("A1").CurrentRegion.Address, Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion12
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1")
.PivotFields("Department").Orientation = xlRowField
.PivotFields("Division").Orientation = xlPageField
.PivotFields("Category").Orientation = xlPageField
.PivotFields("Budget").Orientation = xlDataField
.PivotFields("Actual").Orientation = xlDataField
.PivotFields("Month").Orientation = xlColumnField
.DataPivotField.Orientation = xlRowField
.CalculatedFields.Add "Variance", "=Budget-Actual"
.PivotFields("Variance").Orientation = xlDataField
.DataBodyRange.NumberFormat = "0,000"
.TableStyle = TableStyle2
.DisplayFieldCaptions = False
.PivotFields("Sum of Budget").Caption = " Budget"
.PivotFields("Sum of Actual").Caption = " Actual"
.PivotFields("Sum of Variance").Caption = " Variance"
End With
End Sub
PivotGrid control for creating pivot table
ReplyDelete