Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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"


View products that this article applies to.

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

↑ Back to the top


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.

↑ Back to the top


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

↑ Back to the top


Status

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

↑ Back to the top


Keywords: KB940166, kbexpertiseinter, kbprb, kbtshoot, kberrmsg

↑ Back to the top

Article Info
Article ID : 940166
Revision : 1
Created on : 8/30/2007
Published on : 8/30/2007
Exists online : False
Views : 309