Need help with a Pivot Table Macro from another Workbook

aperaza

New Member
Joined
Apr 7, 2015
Messages
1
Hey everyone,

I'm working on getting this code to work from another workbook. The code works flawlessly on its own when used within the same workbook. My struggle is that I have to export new workbooks every day from our ERP system, but I do not want to copy and paste the code into the new workbook every single time. Plus, it makes it easier for my coworkers if it stays in the same place.

I worked around this by placing all my codes in a master workbook('HTS Commodities Codes'), which we all share for information, and then call upon macros in the new workbook that automate the vlookups and also to format the data for the pivot table. The error I keep receiving is 'Subscript is out of range.' So I know it has to do with the source, but any changes I make do not help. The first answer is obviously get a new ERP system, however, I just need to deal with this for another year before it becomes a reality.

Master Workbook: 'HTS Commodity Codes'
-has all the codes in "This Workbook"

New Workbooks: Names are Order Numbers. Worksheets are always named “SQL QUERY”

Any help is greatly appreciated!

Code:
Sub CreatePivot()
Dim WSD As Worksheet
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim PRange As Range
Dim FinalRow As Long
 
Set WSD = Worksheets("SQL QUERY")
Dim WSR As Worksheet
 
FinalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
FinalCol = WSD.Cells(1, Application.Columns.Count). _
End(xlToLeft).Column
Set PRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:= _
xlDatabase, SourceData:=PRange.Address)
 
 
Set PT = PTCache.CreatePivotTable(TableDestination:=Worksheets("SQL QUERY").Range("N2"))
 
 
PT.ManualUpdate = True
 
 
PT.AddFields RowFields:="HTS2"
 
 
With PT.PivotFields("QUANTITY")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
.NumberFormat = "#,##0"
.Name = "Sum of Quantity"
End With
 
With PT.PivotFields("AMOUNT")
.Orientation = xlDataField
.Function = xlSum
.Position = 2
.NumberFormat = "$ #,##0.00"
.Name = "Sum of Amount"
End With
 
PT.NullString = "0"
 
PT.ManualUpdate = False
PT.ManualUpdate = True
 
 
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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