无效的采购或论点

时间:2020-05-22 20:22:16

标签: excel vba pivot-table

我有一个广泛的Excel数据透视表宏,可创建6个数据透视表。每当我尝试运行宏时,都会收到错误消息(5),无效的采购或参数。我浏览了各种论坛,但找不到针对此错误的修复程序。该错误驻留在第3-6行中。如果有人能指出正确的方向,我将非常感激。我不精通VBA语言。

Range("A5").Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "All Open Tool Records 05-22-20!R1C1:R4750C27", Version:= _
    xlPivotTableVersion10).CreatePivotTable TableDestination:="Sheet1!R3C1", _
    TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
Sheets("Sheet1").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Department")
    .Orientation = xlRowField
    .Position = 1
End With

1 个答案:

答案 0 :(得分:0)

更好地使用变量来捕获流程中的各种对象-使得整个事情更易于管理:

Dim pc As PivotCache
Dim pt As PivotTable
dim ws As Worksheet

Set ws = Sheets.Add() 'assuming this is where the pivot table goes....

Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
            SourceData:= "All Open Tool Records 05-22-20!R1C1:R4750C27", _
            Version:= xlPivotTableVersion10)

Set pt = pc.CreatePivotTable(TableDestination:=ws.Range("A3"), _
           TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10)

With pt.PivotFields("Department")
    .Orientation = xlRowField
    .Position = 1
End With