Pivot Table W/ multiple sources VBA Problem

Bigzippy

Board Regular
Joined
Jul 1, 2003
Messages
182
I am trying to use named ranges from multiple worksheets to create a pivot table but it will not work. Any ideas?

CODE:
Sheets("sheet1").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlConsolidation, SourceData:= _
Array("name", "name", "name", "name", "name", "name", "name", _
"name", "name")).
CreatePivotTable TableDestination:=Range("A1"), _
TableName:="PivotTable6"
With ActiveSheet.PivotTables("PivotTable6")
.ColumnGrand = False
.HasAutoFormat = False
.RowGrand = False
.SmallGrid = False
End With
ActiveSheet.PivotTables("PivotTable6").PivotFields("Data").PivotItems( _
"Sum of Value").Position = 1
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Bigzippy

Board Regular
Joined
Jul 1, 2003
Messages
182
Anyone know if this can be done?
Makes my book more efficient not having to copy all these ranges to sheet1
 

Bigzippy

Board Regular
Joined
Jul 1, 2003
Messages
182
Solution

Sub test()
Application.ScreenUpdating = False
Dim wsn As String
Dim i As Integer
Dim prange As String
Dim PCrange As String
Dim rangearr() As String

ReDim Preserve rangearr(i To Worksheets.Count - 5)

For i = 2 To Worksheets.Count - 3 Step 1
wsn = Worksheets(i).Name
Sheets(wsn).Select
Range("B9:D9").Select
Range(Selection, Selection.End(xlDown)).Select
prange = Selection.Address(ReferenceStyle:=xlR1C1, _
RowAbsolute:=True, _
ColumnAbsolute:=True)
PCrange = wsn & "!" & prange
If (i - 3) < 0 Then rangearr(0) = PCrange _
Else: rangearr(LBound(rangearr) + (i - 2)) = PCrange
Next i

Sheets("sheet1").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlConsolidation, SourceData:= _
rangearr).CreatePivotTable TableDestination:=Range("a1"), _
TableName:="PivotTable1"
With ActiveSheet.PivotTables("PivotTable1")
.ColumnGrand = False
.HasAutoFormat = False
.RowGrand = False
.SmallGrid = False
End With
ActiveSheet.PivotTables("PivotTable1").SmallGrid = False
ActiveSheet.PivotTables("PivotTable1").PivotFields("Data").PivotItems( _
"Sum of Value").Position = 1

Range("D1").Select
Application.CommandBars("PivotTable").Visible = False

End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,160
Messages
5,768,530
Members
425,480
Latest member
br400821

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
Top