Error message when you play a recorded macro to create a PivotTable in a new worksheet in Excel 2007: "Run-time error '1004': Application-defined error" or "Run-time error '5': Invalid procedure call or argument"

Symptoms

In Microsoft Office Excel 2007, you have a recorded macro that performs the following operations:
  • Create a new PivotTable from the data table in a worksheet.
  • Put the new PivotTable into a new worksheet.
When you play the recorded macro, you receive one of the following error messages:
  • Error message 1
    Run-time error '1004': Application-defined error.
  • Error message 2
    Run-time error '5': Invalid procedure call or argument.
Note The following code is an example of a recorded macro.
Sub Macro1()
'
' Macro1 Macro
'
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R12C2", Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="Sheet4!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion12
Sheets("Sheet4").Select
Cells(3, 1).Select
End Sub

Cause

This problem occurs because the destination worksheet and the range for the new PivotTable are specified in the recorded macro. Therefore, the recorded macro is not dynamic. Additionally, the recorded macro cannot be played repeatedly.

Resolution

To resolve this problem, follow these steps:
  1. In the Microsoft Visual Basic Editor (VBE), locate the code for the recorded macro.
  2. Empty the value of the TableDestination variable as in the following example.
       TableDestination=""
  3. Remove the following lines from the code.
       Sheets.add
     Sheets ("sheetname").Select
    Cells (x,y).Select
  4. On the File menu, click Save.
Note The modified code for the example in the Symptoms section resembles the following.
Sub Macro1()
'
' Macro1 Macro
'
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R12C2", Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion12
End Sub

Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.
內容

文章識別碼:940166 - 最後檢閱時間:2007年8月30日 - 修訂: 1

意見反應