I have a snippet of code that is run from MS project to extract data and then creates a pivot table in excel. This is working fine. When I tried to add a slicer to that pivot table, the icon appears to be greyed out. I googled over the internet and found out that it is related to older versions of excel and it can be fixed when file is saved in compatibility mode. My problem is unique in that sense because I have Microsoft365 subscription so I am already using the latest version of excel....
To narrow down my problem, my pivot tables are getting created as an older version and I don't have a clue. I have shared my code to have some feedback please.
I have added following reference to excel
Sub AddReferenceToExcel()
'this sub adds the correct reference to excel to ensure that the export works
Dim Major As Long
Dim Minor As Long
On Error Resume Next
Major = 1
With ActiveProject.VBProject.References
.Remove .Item("Excel")
Major = 1
For Minor = 9 To 5 Step -1
Err.Clear
.AddFromGuid "{00020813-0000-0000-C000-000000000046}", Major, Minor
If Err.Number = 0 Then
Exit For 'no error, so reference set
End If
Next Minor
End With
End Sub
'Creating Pivot table
'====================
Dim pt As PivotTable
Dim ptfield As PivotField
Dim strField As String
Dim xlSheet As Excel.Worksheet
Set xlSheet = xlAPP.Worksheets(1)
Dim PTOutput As Excel.Worksheet
Dim PTCache As PivotCache
Dim PRange As Range
Dim finalRow As Variant
Dim finalCol As Variant
'Setting Pivot Range on Sheet1
'=============================
xlSheet.Activate
' Find the last row with data
finalRow = xlSheet.Cells(xlAPP.Rows.Count, 3).End(xlUp).Row - 3
' Find the last column with data
finalCol = xlSheet.Cells(4, xlAPP.Columns.Count).End(xlToLeft).Column
' Find the range of the data
Set PRange = xlSheet.Cells(4, 1).Resize(finalRow, finalCol)
ActiveSheet.ListObjects.Add(xlSrcRange, PRange, , xlYes).Name = "T_PivotData"
Set PTOutput = xlAPP.Worksheets(3)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:="T_PivotData")
'Create the pivot table
Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutput.Cells(20, 1), TableName:="ForecastPivotFTE")