在旧版本的 Excel 中创建的 MS Project 数据透视表

问题描述 投票:0回答:1

我有一段从 MS 项目运行的代码片段,用于提取数据,然后在 Excel 中创建数据透视表。这工作正常。当我尝试向该数据透视表添加切片器时,该图标显示为灰色。我在互联网上搜索,发现它与旧版本的Excel有关,并且当文件以兼容模式保存时可以修复。 从这个意义上说,我的问题是独一无二的,因为我有 Microsoft365 订阅,所以我已经在使用最新版本的 excel....

为了缩小我的问题范围,我的数据透视表是作为旧版本创建的,我没有任何线索。 我已经分享了我的代码,请提供一些反馈。

我添加了以下对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")

excel vba pivot-table ms-project
1个回答
0
投票

您面临的问题可能是由于在

AddReferenceToExcel
子例程中添加 Excel 引用的方式造成的。该循环尝试从旧版本的 Excel 开始添加引用,并在找到有效的版本时停止。这可能会导致您的代码引用旧版本的 Excel,即使您安装了最新版本也是如此。

解决此问题的方法如下:

  1. 显式设置 Excel 版本:不要循环访问旧版本,而是显式设置对最新版本的引用。对于 Microsoft 365,您可能想要使用版本 16。以下是修改
    AddReferenceToExcel
    子例程的方法:
Sub AddReferenceToExcel()
    'this sub adds the correct reference to excel to ensure that the export works
    On Error Resume Next
    With ActiveProject.VBProject.References
        .Remove .Item("Excel")
        .AddFromGuid "{00020813-0000-0000-C000-000000000046}", 1, 16 ' Explicitly set to version 16
    End With
End Sub
  1. 使用特定于版本的对象变量:不要使用

    PivotTable
    PivotField
    等通用对象类型,而是尝试使用
    Excel.PivotTable
    Excel.PivotField
    等特定于版本的对象类型。这可以确保您正在使用最新版本的对象。

  2. 更新数据透视缓存创建:创建数据透视缓存时,您将使用

    ActiveWorkbook.PivotCaches.Add
    。相反,请尝试使用
    xlAPP
    参考来确保您使用的是正确的 Excel 实例:

Set PTCache = xlAPP.ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="T_PivotData")
  1. 以现代 Excel 格式保存:创建数据透视表后,以现代 Excel 格式保存工作簿 (
    .xlsx
    )。这可确保文件不处于兼容模式:
xlAPP.ActiveWorkbook.SaveAs "YourFilePath.xlsx", FileFormat:=51 '51 corresponds to .xlsx format
  1. 检查 Excel 加载项:有时,某些加载项可能会干扰 Excel 的功能。尝试一一禁用加载项,看看是否有任何加载项导致了问题。

  2. 更新 Office:确保您的 Microsoft 365 订阅是最新的。有时,更新可以解决兼容性和功能问题。

  3. 在 Excel 中重新创建数据透视表:作为最后的手段,将数据导出到 Excel 后,在 Excel 中手动创建数据透视表以查看切片器选项是否可用。这可以帮助您确定问题是出在 VBA 代码还是 Excel 本身。

如果您已尝试所有这些建议并且问题仍然存在,则可能还有其他因素在起作用,并且需要更深入地了解整个代码和环境。

© www.soinside.com 2019 - 2024. All rights reserved.