|
-
Nov 16th, 2009, 07:17 AM
#1
Pivot Table Source
Anyone know how to set the source of one pivottable to that of another using VBA?
-
Nov 16th, 2009, 07:36 AM
#2
Re: Pivot Table Source
Bush, you can record a macro to get what you want.
If you are setting up the 1st pivot using vba then store the source in a variable and use that variable to set the source of the 2nd pivot...
For example
Code:
'~~> Any cell in the 2nd Pivot. I have taken H10 as example
Range("H10").Select
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:=MyVariable
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Nov 16th, 2009, 08:08 AM
#3
Re: Pivot Table Source
Yeah, I've had a crack with that before - the code it gives you when you record the macro can't be applied it just errors for me.
The code it gives me:
Code:
Range("E8").Select
ActiveSheet.PivotTableWizard SourceType:=xlPivotTable, SourceData:= _
"PivotTable1"
So I amend it to the below and try it on a different sheet:
Code:
Worksheets("Sheet1").Activate
ActiveSheet.Range("E8").Select
ActiveSheet.PivotTableWizard SourceType:=xlPivotTable, SourceData:="PivotTable1"
but that gives error PivotTableWizard method of Worksheet class failed
alternatively I've tried:
Code:
Range("E8").Select
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
Workseets("Sheet1").PivotTables("PivotTable1").SourceData
which gives and Application-defined or object-defined error
-
Nov 16th, 2009, 08:11 AM
#4
Re: Pivot Table Source
No Bush... I didn't mean that...
How are you creating the 1st pivot?
This would give you an error as it requires a range or a variable which has range address stored in it...
SourceData:="PivotTable1"
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Nov 16th, 2009, 08:13 AM
#5
Re: Pivot Table Source
both pivots already exist in the spreadsheet
-
Nov 16th, 2009, 08:27 AM
#6
Re: Pivot Table Source
This is to get the names of the pivot tables in your excel sheet
Code:
Sub GetPivotNames()
Dim pvt As PivotTable
'~~> Getting the names of your Pivot
For Each pvt In ActiveSheet.PivotTables
MsgBox pvt.Name
Next
End Sub
Once you have the names simply replace it in the code below...
Code:
Sub GetSetPivotSourceData()
Dim pvt As PivotTable, rng1 As String
'~~> Getting the sourcedata from the first Pivot
For Each pvt In ActiveSheet.PivotTables
If Trim(pvt.Name) = "PivotTable1" Then
rng1 = pvt.SourceData
End If
Next
'~~> Setting the sourcedata for the 2nd Pivot
For Each pvt In ActiveSheet.PivotTables
If Trim(pvt.Name) = "PivotTable2" Then
pvt.SourceData = rng1
End If
Next
End Sub
Last edited by Siddharth Rout; Nov 16th, 2009 at 08:34 AM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Nov 16th, 2009, 09:36 AM
#7
Re: Pivot Table Source
That doesn't seem to work. I get an "Application-defined or object defined error" trying to access the Source Data directly e.g.:
Code:
Dim S As String
S = Worksheets("Sheet1").PivotTables("PivotTable1").SourceData
However, I think I've solved my problem another way. I was trying to change the access database source of one pivottable (by changing the connection string) and refresh all the other pivots at the same time (by maintaining the link between the pivots).
If you do this manually through the front end then it dissociates the pivot you've just refreshed from the others, so I was trying to repoint all the other pivots back to the refreshed one.
But by making the change directly on the PivotCache connection string, they all seem to have updated:
Code:
Worksheets("Sheet1").PivotTables("PivotTable1").PivotCache.Connection = 'New Connection
Worksheets("Sheet1").PivotTables("PivotTable1").RefreshTable
-
Nov 16th, 2009, 10:16 AM
#8
Re: Pivot Table Source
Another way to do it...
vb Code:
Sub Sample() Dim pvt As PivotTable, pvt1 As PivotTable Set pvt = Worksheets("Sheet1").PivotTables("PivotTable1") Set pvt1 = Worksheets("Sheet1").PivotTables("PivotTable2") pvt1.SourceData = pvt.SourceData End Sub
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|