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

时间:2017-08-02 13:35:55

标签: excel vba excel-vba

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

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

我尝试了一些修复,比如替换'和',清空Tabledestination字段,更改数据透视表版本等等。我根本不熟悉VBA,所以我不知道还有什么地方可以放我的在调试器告诉我的地方旁边。

这是调试器提供错误的地方:

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

提前感谢您的帮助。

编辑:尝试按照Graham的建议拆分函数,得到运行时错误'424'对象需要。以下是我如何进行拆分。

Edit2 :将下面的代码更新到最后一个版本,它现在提供以下错误“运行时错误'1004':数据透视表字段名称无效。要创建数据透视表,您必须使用组织为带有标记列的列表的数据。如果要更改数据透视表字段的名称,则必须为该字段键入新名称。“在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")

2 个答案:

答案 0 :(得分:1)

首先,非常感谢@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”并用“Sheet2”替换的所有实例。小心这一点,我注意到如果我从Excel运行代码,新生成的工作表称为“Sheet1”,如果由HPQC运行,则称为“Sheet2”。确保使用调试器并查看新工作表的调用方式。

另外要注意,对于“Set pivotRange”和“Set tableRange”,你不能给它自己的工作表名称,将工作表分配给变量然后使用它。

结合所有这些东西,代码现在可以运行了。后处理仍然看起来不像我预期的那样,但我怀疑这是由于ExcelVBA从Excel 2007更改为Excel2013(数据透视表显示所需的信息,但格式略有不同)。我希望这会在同样的情况下帮助其他人。

答案 1 :(得分:0)

查看PivotCaches.create here的当前文档,你会发现两个版本的方法签名都是这样的:

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: Type Mismatch with PivotCaches