VBA 中的运行时错误 5:无效的过程调用或参数

问题描述 投票:0回答:1
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

我尝试使用每个版本创建表,但它不起作用。

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

尝试将工作表名称用单引号引起来...

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")

如您所见,它首先创建数据透视缓存,然后创建数据透视表。

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