VBA How to use variables to extract pivot table data

Matador_24

Board Regular
Joined
Dec 1, 2011
Messages
205
Hello,

I am working on a code that extracts data from a pivot table.

When using this code, I get the value I want to obtain, basically I obtain the value of Account number 10003 for the period 3 of Year 2017 from a complex pivot table:

Code:
LngValue = ActiveSheet.PivotTables(1).GetPivotData("Amount", "Account Number", "10003", "Period Year", "2,016", "Period Month", "3")


Now, the account number will change depending on data and thus, I need to replace the 10003 by a variable in the code.
However, I have an error on the syntax when using the variable LngAccount:

Code:
Dim LngValue As Long
Dim LngAccount As Long

LngAccount = 10003
MsgBox "xx" & LngAccount & "xx"
LngValue = ActiveSheet.PivotTables(1).GetPivotData("Amount", "Account Number", LngAccount, "Period Year", "2,016", "Period Month", "3")

Does anybody know how to use a variable with the above code??????
note that the result of the msgbox is xx10003xx, meaning that the value of LngAccount is 10003, I did it to test if the format was fine.
I have searched on line but I have not been able to find something similar,

Help please!

Thanks,

Luis
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi Luis

Are you sure the first formula worked?
Code:
LngValue = ActiveSheet.PivotTables(1).GetPivotData("Amount", "Account Number", "10003", "Period Year", "[COLOR=#ff0000]2,016[/COLOR]", "Period Month", "3")

I think Period Year should be "2016", not "2,016"

M.
 
Last edited:
Upvote 0
Hello,

Yes, sure it worked, "2,016" is the format of the origin data base for the columns years, it is not ideal but it was set up that way and I do not need to modify it for now....

Thanks,

Luis
 
Upvote 0
I'm asking because i created a very simple pivot table and this worked perfectly for me (Period Year as Numbers)

Code:
Sub aTest()
Dim LngValue As Long
Dim LngAccount As Long

LngAccount = 10003
LngValue = ActiveSheet.PivotTables(1).GetPivotData("Amount", "Account Number", LngAccount, "Period Year", "2016", "Period Month", "3")
MsgBox LngValue
End Sub

Pivot table


A
B
C
D
1
2
3
Account Number​
Period Year​
Period Month​
Sum of Amount​
4
10001​
2.016​
2​
10​
5
3​
16​
6
10002​
2.016​
2​
12​
7
3​
18​
8
10003​
2.016​
2​
14​
9
3​
20​
10
Grand Total​
90​
11

M.
 
Last edited:
Upvote 0
Complementing my previous post: in my Regional Setting the . (dot) in 2.106 is thousands separators (not comma)

M.
 
Upvote 0
Hello,

I tried with "2016" and still the error, it shows the error:
Run-time error '1004': Application-defined or object-defined error

It highlights that line of code. So, I wonder what the issue is if for you works fine..... what can you think of?



The pivot table is more complex, basically Account number is row and for columns I have year (2015, 2016 and 2017) and month (3,6,9,12) it is in the below format, thus slightly different than yours. I wonder if that makes a difference compared with yours?:

Sum of Amount

Period YearPeriod Month









2,0152,015 Total2,016
2,016 Total2,017
2,017 TotalGrand Total
Account GLAccount NumberAccount Name12
312
36

10000 - Assets10002Asset - Cash627,507.78627,507.78(133,762.24)(39,504.92)77,296.43(102,026.70)(101,441.51)(203,468.21)501,336.00

10003Asset - Accounts Receivable15,894.8715,894.87(17,155.42)832.42(8,658.77)(3,759.14)(1,810.38)(5,569.52)1,666.58

10004Asset - Prepaid Expense55,427.3755,427.3793,656.97(6,789.58)4,809.06(164.44)29,220.3629,055.9289,292.35

10006Asset - Building/Land9,301,947.739,301,947.734,679.75(186,662.25)(1,011,891.22)6,889.355,960.0012,849.358,302,905.86

10008Asset - Accumulated Depreciation(1,618,609.59)(1,618,609.59)(39,487.03)154,436.7035,975.64(35,167.67)(35,167.67)(70,335.34)(1,652,969.29)
10000 - Assets Total

8,382,168.168,382,168.16(92,067.97)(77,687.63)(902,468.86)(134,228.60)(103,239.20)(237,467.80)7,242,231.50
20000 - Liabilities20001Liabilities - Accounts Payable(71,152.00)(71,152.00)(203.40)38,090.9611,785.46(7,040.08)14,455.967,415.88(51,950.66)

20002Liabilities - Deferred Income(23,659.97)(23,659.97)(2,314.22)(1,000.00)(265.93)(1,421.98)(59.93)(1,481.91)(25,407.81)

20003Liabilities - Mortgage(562,033.87)(562,033.87)

562,033.87


0.00

20004Liabilities - Other Payable(357,380.06)(357,380.06)5,553.132,157.8936,305.964,297.675,759.5910,057.26(311,016.84)
20000 - Liabilities Total

(1,014,225.90)(1,014,225.90)3,035.5139,248.85609,859.36(4,164.39)20,155.6215,991.23(388,375.31)

<tbody>
</tbody>


Anything else you can think of? Could you do a test with a simpler table but with the above fields? (Year, month as columns and values as what I want to extract).


Thanks,

Luis
 
Upvote 0
Hello,

I think I might have found one part of the issue, I changed to

Code:
Dim LngAccount As [B]String
[/B]

I would think that the source data of that field is text not number as I thought. The issue is that I need value type as I will use also rules for '<than or="">' eventually that I will not be able to use with text. The issue is that it seems that for that code, the variable must be text and not value that is why i think It was showing me error when the account number as Long....

I am also surprised that the code worked for you if it was defined as Long.

How can make the code accept the Long value instead of string?</than>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,575
Members
449,039
Latest member
Arbind kumar

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