'Create a new Sheet If WorkSheetExists("myPivotWS1") Then Application.DisplayAlerts = False Application.Worksheets("myPivotWS1").Delete() Application.DisplayAlerts = True End If myPivotWS = Application.Worksheets.Add() myPivotWS.Name = "myPivotWS1" '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("Item").Orientation = Excel.XlPivotFieldOrientation.xlPageField myPivotTable.PivotFields("Category").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 myPivotTable.PivotFields("Actual").Orientation = Excel.XlPivotFieldOrientation.xlDataField 'Add a calculated field myPivotTable.CalculatedFields.Add("Variance", "=Budget-Actual") myPivotTable.PivotFields("Variance").Orientation = Excel.XlPivotFieldOrientation.xlDataField 'this rotates the pivot 90 degrees myPivotTable.DataPivotField.Orientation = Excel.XlPivotFieldOrientation.xlRowField 'Format Numbers myPivotTable.DataBodyRange.NumberFormat = "$ #,##0.00;[Red]-$ #,##0.00" 'Style myPivotTable.TableStyle2 = "PivotStyleMedium3" myPivotTable.DisplayFieldCaptions = False 'Change the captions myPivotTable.PivotFields("Sum of Actual").Caption = " Actual" myPivotTable.PivotFields("Sum of Budget").Caption = " Budget" myPivotTable.PivotFields("Sum of Variance").Caption = " Variance"