'Declare the pivot objects here Dim myPivotWS As Excel.Worksheet Dim myPivotCache As Excel.PivotCache Dim myPivotTable As Microsoft.Office.Interop.Excel.PivotTable 'Create a new Sheet If WorkSheetExists("myPivotWS") Then Application.DisplayAlerts = False Application.Worksheets("myPivotWS").Delete() Application.DisplayAlerts = True End If myPivotWS = Application.Worksheets.Add() myPivotWS.Name = "myPivotWS" 'Create the PivotCache (holds the pivot data) myPivotCache = Application.ActiveWorkbook.PivotCaches.Create(Excel.XlPivotTableSourceType.xlDatabase, "BudgetDataLO") 'Create the PivotTable on the screen myPivotTable = myPivotCache.CreatePivotTable(myPivotWS.Range("A1"), "myPivotCache") 'Add & set fields myPivotTable.PivotFields("Category").Orientation = Excel.XlPivotFieldOrientation.xlPageField myPivotTable.PivotFields("Item").Orientation = Excel.XlPivotFieldOrientation.xlPageField myPivotTable.PivotFields("Month").Orientation = Excel.XlPivotFieldOrientation.xlPageField myPivotTable.PivotFields("Department").Orientation = Excel.XlPivotFieldOrientation.xlColumnField myPivotTable.PivotFields("Division").Orientation = Excel.XlPivotFieldOrientation.xlRowField myPivotTable.PivotFields("Budget").Orientation = Excel.XlPivotFieldOrientation.xlDataField 'Add a calculated field myPivotTable.CalculatedFields.Add("Variance", "=Budget-Actual") myPivotTable.PivotFields("Variance").Orientation = Excel.XlPivotFieldOrientation.xlDataField 'this swaps rows and columns - if needed myPivotTable.DataPivotField.Orientation = Excel.XlPivotFieldOrientation.xlRowField 'Format Numbers myPivotTable.DataBodyRange.NumberFormat = "[Black]$ #,##0;[Black]$ #,##0" 'Style myPivotTable.TableStyle2 = "PivotStyleDark6" myPivotTable.DisplayFieldCaptions = False 'Change the captions myPivotTable.PivotFields("Sum of Budget").Caption = " Budget" myPivotTable.PivotFields("Sum of Variance").Caption = " Variance"