Add a PivotTable using VBA and adding to the Data Model

Confidentjohn

Board Regular
Joined
Mar 3, 2009
Messages
73
Hi

I want to set a pivottable and also use the DataModel feature (Excel 2013)

If I record the VBA I get the code I need however it is set to the specific sheet


Code:
Sub Macro4()'
' Macro4 Macro
'


'
    Workbooks("April 2015 - orderDATA.xlsx").Connections.Add2 _
        "WorksheetConnection_Sheet1!$A$1:$AC$129180", "", _
        "WORKSHEET;C:\Users\jmitchel\My Docs\[April 2015 - orderDATA.xlsx]Sheet1" _
        , "Sheet1!$A$1:$AC$129180", 7, True, False
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
        ActiveWorkbook.Connections("WorksheetConnection_Sheet1!$A$1:$AC$129180"), _
        Version:=xlPivotTableVersion15).CreatePivotTable TableDestination:= _
        "Sheet3!R3C1", TableName:="PivotTable2", DefaultVersion:= _
        xlPivotTableVersion15
    Sheets("Sheet3").Select
    Cells(3, 1).Select
End Sub

I would like to reuse this Macro for other documents I have so ideally i would like to remove specific references to the doc and just have the macro run against the active sheet, Could anyone help me edit the code?

Cheers

Joh
 

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.

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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