Excel VBA函数给出错误“无效的过程调用或参数”

我的同事使用下面的function从HP Quality Center生成的Excel文件中提取数据。 由于某种原因,它适用于Excel 2007中的他,但不适用于Excel 2013中的我。

我得到错误无效的过程调用或参数

我已经尝试了一些修复,如replace“和”,清空Tabledestination字段,更改数据透视表版本和许多其他的东西。我不熟练的VBA,所以我不知道还有什么地方把我的手放在哪里debugging器告诉我。

这是debugging器给出错误的地方:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "Query1!R1C1:R5000C10", Version:=xlPivotTableVersion10).CreatePivotTable _ TableDestination:="Sheet4!R3C1", TableName:="Tabella_pivot1", _ DefaultVersion:=xlPivotTableVersion10 

完整的代码如下:

 Sub QC_PostProcessing() Dim MainWorksheet As Worksheet ' Make sure your worksheet name matches! Set MainWorksheet = ActiveWorkbook.Worksheets("Query1") Dim DataRange As Range Set DataRange = MainWorksheet.UsedRange ' Now that you have the data in DataRange you can process it. ' Table Title Range("A1:J1").Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.399975585192419 .PatternTintAndShade = 0 End With With Selection.Font .ThemeColor = xlThemeColorDark1 .TintAndShade = 0 End With Selection.Font.Bold = True ' Autofit Columns("A:J").EntireColumn.AutoFit ' Table grid Range("A1:J1").Select Range(Selection, Selection.End(xlDown)).Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With ' Pivot Dim Data_sht As Worksheet Dim StartPoint As Range Dim DataRange2 As Range Dim NewRange As String Set Data_sht = ThisWorkbook.Worksheets("Query1") Set StartPoint = Data_sht.Range("A1") Set DataRange2 = Data_sht.Range(StartPoint, StartPoint.SpecialCells(xlLastCell)) NewRange = Data_sht.Name & "!" & _ DataRange2.Address(ReferenceStyle:=xlR1C1) 'Query1!R1C1:R5000C10" Selection.Copy Application.CutCopyMode = False Sheets.Add ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "Query1!R1C1:R5000C10", Version:=xlPivotTableVersion10).CreatePivotTable _ ' variabile TableDestination:="Sheet4!R3C1", TableName:="Tabella_pivot1", _ DefaultVersion:=xlPivotTableVersion10 Sheets("Sheet4").Select Cells(3, 1).Select With ActiveSheet.PivotTables("Tabella_pivot1").PivotFields("Priority") .Orientation = xlRowField .Position = 1 End With With ActiveSheet.PivotTables("Tabella_pivot1").PivotFields( _ "Subject") .Orientation = xlRowField .Position = 2 End With ActiveSheet.PivotTables("Tabella_pivot1").AddDataField ActiveSheet.PivotTables( _ "Tabella_pivot1").PivotFields("Status"), "Count of Status", xlCount With ActiveSheet.PivotTables("Tabella_pivot1").PivotFields("Status") .Orientation = xlColumnField .Position = 1 End With ' Graph Range("K3").Select ActiveSheet.PivotTables("Tabella_pivot1").PivotSelect "", xlDataAndLabel, True ActiveSheet.Shapes.AddChart.Select ActiveChart.ChartType = xlColumnClustered ActiveChart.SetSourceData Source:=Range("Sheet4!$A$3:$E$22") ActiveSheet.Shapes("Chart 1").IncrementLeft 200 ActiveSheet.Shapes("Chart 1").IncrementTop -230.00 ActiveSheet.Shapes("Chart 1").ScaleWidth 1.3270833333, msoFalse, _ msoScaleFromTopLeft ActiveSheet.Shapes("Chart 1").ScaleHeight 1.3767362934, msoFalse, _ msoScaleFromTopLeft ' Rename sheets Sheets("Sheet4").Select Sheets("Sheet4").Name = "Stats" Sheets("Query1").Select Sheets("Query1").Name = "Report" Sheets("Stats").Select Sheets("Stats").Move After:=Sheets(2) Range("A1").Select Sheets("Report").Select Range("A2").Select End Sub 

预先感谢您的帮助。

编辑 :试图分裂的function,格雷厄姆build议,得到一个运行时错误'424'所需的对象。 这是我如何做分裂。

编辑2 :将下面的代码更新到最新版本,它现在提供以下错误“运行时错误'1004':数据透视表字段名称无效。要创build数据透视表报表,您必须使用数据作为列表带有标签的列如果您要更改数据透视表字段的名称,则必须为该字段键入一个新的名称。 与CreatePivotTable一致。

 Dim pCache As PivotCache Dim pTable As PivotTable Dim pivotRange As Range Dim tableRange As Range Dim Data_sht2 As Worksheet Set Data_sht2 = ThisWorkbook.Worksheets("Sheet1") Set pivotRange = Data_sht.Range(Data_sht.Cells(1, 1), Data_sht.Cells(5000, 18)) Set tableRange = Data_sht2.Range(Data_sht2.Cells(3, 18), Data_sht2.Cells(1, 3)) Set pCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _ SourceData:=pivotRange) Set pTable = pCache.CreatePivotTable(TableDestination:=tableRange, _ TableName:="Tabella_pivot1") 

首先,非常感谢@Graham能够帮助我解决部分问题。 为了解决这个问题,首先我分割代码的一部分给出错误。

 Dim pCache As PivotCache Dim pTable As PivotTable Dim pivotRange As Range Dim tableRange As Range Dim Data_sht2 As Worksheet Set Data_sht2 = ThisWorkbook.Worksheets("Sheet2") Set pivotRange = Data_sht.Range(Data_sht.Cells(1, 1), Data_sht.Cells(5000, 10)) Set tableRange = Data_sht2.Cells(3, 1) Set pCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _ SourceData:=pivotRange) Set pTable = pCache.CreatePivotTable(TableDestination:=tableRange, _ TableName:="Tabella_pivot1") 

确保当你将前一部分转换成这个时,你知道R5000C10的意思是“Row 5000 Column 10”(我不知道,这给我造成了一些问题)。

然后,我改变了原来代码中“Sheet4”被调用并replace为“Sheet2”的所有实例。 注意这一点,我注意到,如果我从Excel中运行代码,新生成的表被称为“Sheet1”,如果由HPQC运行,它被称为“Sheet2”。 确保你使用debugging器,看看新的表格是如何调用的。

另外要注意,对于“设置pivotRange”和“设置tableRange”,你不能给它的工作表名称本身,将工作表分配给一个variables,然后使用它。

所有这些东西结合起来,代码现在可以工作。 后处理仍然看起来不像我期望的,但我怀疑这是由于ExcelVBA从Excel 2007到Excel2013(数据透视表显示需要的信息,但格式稍有不同)的变化。 我希望这会在同样的情况下帮助别人。

看一下这里的PivotCaches.create的当前文档,你会发现这两个版本的方法签名看起来是这样的:

 PivotCache Create( XlPivotTableSourceType SourceType, Object SourceData, Object Version ) 

您将PivotTableVersion设置为10,但Excel 2010和2013的默认值为12:

如果未提供,则数据透视表的版本将为xlPivotTableVersion12。

这几乎肯定会导致您在Excel版本之间移动时遇到的问题,因此删除最后一个参数并将其转换为当前版本的Excel的默认值:

 ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "Query1!R1C1:R5000C10").CreatePivotTable _ TableDestination:="Sheet4!R3C1", TableName:="Tabella_pivot1" 

也可以尝试把这个调用分解成多个语句,就像你在这个问题中接受的答案中看到的一样,它会使它更容易阅读,并且帮助你理解错误的地方:

VBA:types与PivotCaches不匹配