Error in Generating Pivot Table from Macro

samantha87

New Member
Joined
Feb 15, 2016
Messages
4
Hi I keep getting errors (either Range error or invalid procedure call error) when trying to run a pivot table using macro.
I've attempted to insert a single quote to my table destination but it raises another error i.e. invalid procedure call. I don't know where went wrong??
I'm also thinking if i should give the pivot table a name, which im completely lost on how to. I have multiple pivot tables to create for this sheet also, not just this. Is there a way that I can easily define each of them and create it?
Here's my code:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">Dim Lastrow As Integer
Dim Table1 As Object
Dim DataSource As Object

Lastrow
= ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

Set DataSource = ThisWorkbook.ActiveSheet.Range("A1:CF" & Lastrow)
Set Table1 = ThisWorkbook.Sheets("Table1-Data")

DataSource
.Select


<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Selection, Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination
:="'Table1'!R5C1", TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion15</code>
</code></pre>
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What happens if you replace SourceData:=Selection with SourceData:=DataSource and replace TableDestination:="'Table1'!R5C1" with TableDestination:="''Table1-Data''!R5C1"?

By the way, VBA/Excel will name the pivot table automatically.

PS Is the data actually on 'Table-Data' or another worksheet?
 
Upvote 0
Hi Norie,

Thanks for your help. I tried replacing "Selection" with "DataSource" and putting in "-Data" in addition to "Table1". It still doesn't work.

Yes, but noticed that the pivot table cached will rename the pivot table added everytime the macro is being ran.

Yes, the data is actually referring from another worksheet.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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