Invalid procedure or call argument - Pivot table macro

Jdw5511

New Member
Joined
Jul 19, 2016
Messages
20
Hello,

I keep getting the run-time error '5' each time that I attempt to run my pivot table macro. Here is the line of code that keeps producing the error:


ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Temporary!A1:BJ1" & LRow, Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:="Results!R3C1", TableName:="PivotTableBmStats", DefaultVersion:=xlPivotTableVersion15

Any assistance that could be provided would be greatly appreciated!

Thanks,

Justin
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
What's the value of LRow?
Is there already a sheet called Results? If so, does it already have a pivot table on it?
 
Upvote 0
What's the value of LRow?
Is there already a sheet called Results? If so, does it already have a pivot table on it?

Hi Rory,

Thank you for your response.

LRow is the last row variable I have defined. When the code runs to that point it inserts a results sheet that is blank with no pivot table in it.
 
Upvote 0
LRow is the last row variable I have defined

I know that - what I am asking is what is the actual value of the variable when the code runs?
 
Upvote 0
In this specific circumstance, there are two rows that make up the dataset.

Here is the full code up until the error:
Code:
    Dim LRow As Long
    LRow = Sheets("Formatted data").Range("A3").End(xlDown).Row - 1
    
'Copy and paste filtered data from Formatted data tab'
  
    Sheets("Formatted data").Select
    Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
      
    Sheets.Add Before:=ActiveSheet
    ActiveSheet.Name = "Temporary"
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues
         
    LRow = Sheets("Temporary").Range("A2").End(xlDown).Row - 1
      
'Add temporary pivot table to obtain metrics for results tab'
       
    Sheets.Add.Name = "Results"
        
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Temporary!A1:BJ1" & LRow, Version:=xlPivotTableVersion15). _
        CreatePivotTable TableDestination:="'Results'!R3C1", TableName:="PivotTableBmStats" _
        , DefaultVersion:=xlPivotTableVersion15
    Sheets("Results").Select
    Cells(3, 1).Select
 
Last edited by a moderator:
Upvote 0
That still doesn't answer my question, I'm afraid. What is the actual value of LRow when the error occurs? (not what you think it should be, but what it actually is)

You also have a typo in the code - the source range should be:

Rich (BB code):
SourceData:= _
"Temporary!A1:BJ" & LRow

and not:

Rich (BB code):
SourceData:= _
"Temporary!A1:BJ1" & LRow

which could be related to your problem.
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,799
Members
449,337
Latest member
BBV123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top