我试图在将数据从一个工作簿复制到另一个工作簿后创建一个数据透视表 . 这是代码 .

Dim ws As Worksheet
Set ws = Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "PIVOT_RANGE", Version:=xlPivotTableVersion14). _
        CreatePivotTable TableDestination:=ws.Name & "!R1C1", TableName:= _
        "PivotTable2", DefaultVersion:=xlPivotTableVersion14

我每次都看到不同的数据透视表名称 . 有时它显示 PivotTable1 ,有时显示 PivotTable2 .

我也收到以下错误消息

'Run-time error 1004':

The PivotTable field name is not valid. To create a PivotTable report, you must use data that is organized as a list with labeled columns. If you are changing the name of a PivotTable field, you must type a new name for the field.

我实际上有来自A1:X的数据,所有列都有列 Headers ,但我仍然收到此错误消息 .

只是想知道有没有办法通过VBA获取数据透视表名称,而不是在代码中给出表名(即 TableName:= "PivotTable2" ),这在大多数时候都不相同 .

有人可以帮我这个吗?