ActiveSheet.Name = "RReal+RCEmin"
Dim UltimaFila As Long
Dim RangoDatos As String
UltimaFila = Cells(Rows.Count, 3).End(xlUp).Row
RangoDatos = "RReal+RCEmin!R1C1:R" & UltimaFila & "C9"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=RangoDatos, Version:=xlPivotTableVersion8).CreatePivotTable TableDestination:="RReal+RCEmin!R1C15", TableName:="D1", DefaultVersion:=xlPivotTableVersion8
我尝试使用每个版本创建表,但它不起作用。
尝试将工作表名称用单引号引起来...
Dim SheetName As String
Dim UltimaFila As Long
Dim RangoDatos As String
SheetName = "RReal+RCEmin"
ActiveSheet.Name = SheetName
UltimaFila = Cells(Rows.Count, 3).End(xlUp).Row
RangoDatos = "'" & SheetName & "'!R1C1:R" & UltimaFila & "C9"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=RangoDatos).CreatePivotTable tabledestination:="'" & SheetName & "'!R1C15", TableName:="D1"
还有另一种方法...
Dim SheetName As String
Dim RangoDatos As String
Dim UltimaFila As Long
Dim PTCache As PivotCache
Dim PT As PivotTable
SheetName = "RReal+RCEmin"
ActiveSheet.Name = SheetName
UltimaFila = Cells(Rows.Count, 3).End(xlUp).Row
RangoDatos = "'" & SheetName & "'!R1C1:R" & UltimaFila & "C9"
Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=RangoDatos)
Set PT = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, tabledestination:="'" & SheetName & "'!R1C15", TableName:="D1")
如您所见,它首先创建数据透视缓存,然后创建数据透视表。