Microsoft KB Archive/940166

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

Article ID: 940166

Article Last Modified on 8/30/2007

-

APPLIES TO


 * Microsoft Office Excel 2007

-



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:= _       &quot;Sheet1!R1C1:R12C2&quot;, Version:=xlPivotTableVersion12).CreatePivotTable _ TableDestination:=&quot;Sheet4!R3C1&quot;, TableName:=&quot;PivotTable1&quot;, DefaultVersion _ :=xlPivotTableVersion12 Sheets(&quot;Sheet4&quot;).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:  In the Microsoft Visual Basic Editor (VBE), locate the code for the recorded macro.  Empty the value of the TableDestination variable as in the following example. TableDestination=&quot;&quot;   Remove the following lines from the code. Sheets.add Sheets (&quot;sheetname&quot;).Select Cells (x,y).Select  On the File menu, click Save.</li></ol>

Note The modified code for the example in the Symptoms section resembles the following. Sub Macro1 ' ' Macro1 Macro '   ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _        &quot;Sheet1!R1C1:R12C2&quot;, Version:=xlPivotTableVersion12).CreatePivotTable _ TableDestination:=&quot;&quot;, TableName:=&quot;PivotTable1&quot;, DefaultVersion _ :=xlPivotTableVersion12 End Sub

<div class="status_section">

STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.

Keywords: kberrmsg kbtshoot kbprb kbexpertiseinter KB940166

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.