.GetPivotData makes me crazy, please help

George___

New Member
Joined
Sep 2, 2019
Messages
2
Hallo,

I am quite newbie in writing VBA codes. I use VBA occasionally in my work but it saves me many times. Now I am facing issue with .GetPivotData which I am trying to use for thefirst time but still get Runtime error 1004. My code is as follows:
Sub data()

Dim IKN_PT As PivotTable
Set IKN_PT = Sheet12.PivotTables(1)



For i = 1 ToIKN_PT.PivotFields("Odběratel").PivotItems.Count

Cells(i,10).Value = IKN_PT.PivotFields("Odběratel").PivotItems(i).Name
Next i


For j = 1 ToIKN_PT.PivotFields("Odběratel").PivotItems.Count


Dim OdberatelAs Range
Set Odberatel =Cells(j, 10)

Dim IKN_suma AsRange
Set IKN_suma =IKN_PT.GetPivotData(DataField:="Pohledávka CZK",Field1:="Odběratel", Item1:=Odberatel)

Cells(j, 11) =IKN_suma.Value

Next j
End Sub

The raw with bold end with error 1004. When I replace “Odberatel”with “Range(“J1”) code works but logically with absolute reference to "J1" and Iwant to place relative reference to J column. I have tried many ways, go through forums,etc. For example I have tried:


Set IKN_suma = IKN_PT.GetPivotData("PohledávkaCZK", "Odběratel", Cells(j, 10).Value)


Set IKN_suma = IKN_PT.GetPivotData("PohledávkaCZK", "Odběratel", Cells(j, 10))


Set IKN_suma = IKN_PT.GetPivotData("PohledávkaCZK", "Odběratel", Range(“J” + j))

Set IKN_suma = IKN_PT.GetPivotData("PohledávkaCZK", "Odběratel", Range(“J” & j))


but nothing works. I am really struggling…. Any ideas?





Thanks a lot in advance.



George






 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi guys,

I have moved forward a bit. My code basically works but Imade stupid mistake. I have filter in my pivot table and I expect that line:


“Cells(i, 10).Value =IKN_PT.PivotFields("Odběratel").PivotItems(i).Name “

takes into effect only filtered Pivotitems and not all of them. And to my luckimmediatelly first PivotItem has no value when my filter in pivottable has beenapplied, therefore error 1004 occurs. So I need to find out way how to list just filtered PivotItems and itwill be working finally. Thanks for advice.

George

 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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