VBA pivot table code excel 2010 vs 2016

MrBoutte

New Member
Joined
Aug 17, 2016
Messages
2
I successfully made a macros in excel 2016, for creating a pivot table but I need to replicate the code for excel 2010. Copying over the code from the 2016 version into the 2010 doesn't work and I've also tried re-writing the code, to no avail. I don't understand what I'm doing wrong.
Here's the excel 2016 version that works perfect
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;">[COLOR=#303336]Sheets[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Add
ActiveWorkbook[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotCaches[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Create[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]SourceType[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336]xlDatabase[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] SourceData[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336] _
    [/COLOR][COLOR=#7D2727]"Superbills!R1C1:R563C86"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] Version[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]6[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]CreatePivotTable TableDestination[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336] _
    [/COLOR][COLOR=#7D2727]"Sheet1!R3C1"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] TableName[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] DefaultVersion[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]6[/COLOR][COLOR=#303336]
Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
Cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]3[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Practice Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Provider Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]2[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Full Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
    [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]3[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]AddDataField ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336] _
    [/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Entry Date"[/COLOR][COLOR=#303336]),[/COLOR][COLOR=#7D2727]"Count of Entry Date"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] xlCount
ActiveCell[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Offset[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]0[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]Range[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"A1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Practice Name"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]ShowDetail _
    [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]False[/COLOR][COLOR=#303336]
Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]Name [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]"Pivot Table"[/COLOR]</code>

now here's my adaption for the 2010 version, that isn't working

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;">[COLOR=#101094]Dim[/COLOR][COLOR=#303336] PTCache [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] PivotCache
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] PT [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] PivotTable

[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] PTCache [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] ActiveWorkbook[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotCaches[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Create[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]SourceType[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336]xlDatabase[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] _
        SourceData[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336]Range [/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Superbills!R1C1:R563C86"[/COLOR][COLOR=#303336]))[/COLOR][COLOR=#303336]

Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
Cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]3[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] PT [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Add[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]PivotCache[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336]PTCache[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] _
        TableDestination[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#303336]Range[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1!R3C1"[/COLOR][COLOR=#303336]),[/COLOR][COLOR=#303336] TableName[/COLOR][COLOR=#303336]:=[/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]

ActiveWorkbook[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]ShowPivotTableFieldList [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]True[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Practice Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Provider Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]2[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Full Name"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Orientation [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] xlRowField
        [/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Position [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]3[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]
    ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]AddDataField ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336] _
        [/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Entry Date"[/COLOR][COLOR=#303336]),[/COLOR][COLOR=#7D2727]"Count of Entry Date"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] xlCount
    ActiveCell[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Offset[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]0[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]Range[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"A1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
    ActiveSheet[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]PivotTables[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"PivotTable1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]PivotFields[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Practice Name"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]ShowDetail _
        [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]False[/COLOR][COLOR=#303336]
    Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
    Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Sheet1"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#303336]Name [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]"Pivot Table"[/COLOR]</code>
here's the compiler errors I'm getting:

"Name argument not found" highlighting
(Version:=6) in PTCache variable.

once that's removed.
I get the following
"Run-time error '1004': Method 'Range' of object '_Global' failed"
highlighting the PTCache variable again.
Code:
[INDENT]Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _ SourceData:=Range("Superbills!R1C1:R563C86"))
[/INDENT]
 

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.
Use SourceData as a string:
Code:
SourceData:="Superbills!R1C1:R563C86"
 
Upvote 0
your suggesting to make SourceData a variable and just injecting that into the ActiveWorkbook.PivotCaches.Create parameters ?
 
Upvote 0
No, I'm saying that this:
Code:
Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _ SourceData:=Range("Superbills!R1C1:R563C86"))
should be this:
Code:
Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Superbills!R1C1:R563C86")
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

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