I’m fairly new to VBA, however I have a programming background. I’m writing VBA to open two excel workbooks, copy the pivot tables and paste special the values into a staging workbook. From there, I need to compare the pivot data and output the differences. However, I’m not sure where to go from there.

**Sample Data**

Pivot 1

Sum of Fruits YearMon Category Group 201205 201206 201207 201208 201209 ... 201401 Apples Green 1.3 2.1 3.0 ... Red 1.0 1.5 2.0 ... Apples Total .. .. .. ... Berries Straw 1 1 1.1 2.0 2.1 ... Black 1 1 1.0 0.9 0.9 ... Berroes Total .. .. .. ...

Pivot 2

Sum of Fruits YearMon Category Group 201206 201207 201208 201209 201210 ... 201402 Apples Green 1.5 2.3 3.2 ... Red 0.8 1.3 1.9 ... Yellow 1 0.9 0.9 1 ... Apples Total .. .. .. ... Berries Straw 1 1 1.3 1.8 2.1 ... Black 1 1 0.9 0.8 0.8 ... Berroes Total .. .. .. ...

Desired Result *(Pivot 2 – Pivot 1)*

Sum of Fruits YearMon Category Group 201205 201206 201207 201208 201209 201210 ... 201402 Apples Green -0.6 -0.7 ... Red -0.7 -0.7 ... Apples Total .. .. .. ... Berries Straw 0 -0.1 -0.7 -0.3 ... Black 0 0 0 -0.1 ... Berries Total .. .. .. ...

My first impulse is to put `YearMon`

and `Group`

into arrays based on each `Category`

, then looping through them to remove attributes which don’t exist in both pivots (ie. yellow apples don’t exist in Pivot 1, and there is no data for `201402`

). I would then loop through each group and do the calculation. This sounds too messy.

I’m currently thinking to create a range for `YearMon`

and `Group`

, then looping through the range, vertically and horizontally, for each `Category`

to compare the values. I’m not sure how to exactly find where each `Category`

stops, or whether I could perhaps use `VLOOKUP`

? Or is there some simpler way to compare two pivots?

**Update 1**

The majority of the comments recommend that I try to use the source data behind the pivot to accomplish this task. I’m able to get the data and following the scheme above, which looks like:

Data 1

Category Group YearMon Value Apples Green 201207 1.3 Apples Green 201208 2.1 Apples Green 201209 3.0 Apples Red 201207 1.0 Apples Red 201208 1.5 Apples Red 201209 2.0 Berries Straw 201205 1.0 Berries Straw 201206 1.0 Berries Straw 201207 1.1 Berries Straw 201208 2.0 Berries Straw 201209 2.1 Berries Black 201205 1.0 Berries Black 201206 1.0 Berries Black 201207 1.0 Berries Black 201208 0.9 Berries Black 201209 0.9

Data 2

Category Group YearMon Value Apples Green 201208 1.5 Apples Green 201209 2.3 Apples Green 201210 3.2 Apples Red 201208 0.8 Apples Red 201209 1.3 Apples Red 201210 1.9 Apples Yellow 201207 1.0 Apples Yellow 201208 0.9 Apples Yellow 201209 0.9 Apples Yellow 201210 1.0 Berries Straw 201206 1.0 Berries Straw 201207 1.0 Berries Straw 201208 1.3 Berries Straw 201209 1.8 Berries Straw 201210 2.1 Berries Black 201206 1.0 Berries Black 201207 1.0 Berries Black 201208 0.9 Berries Black 201209 0.8 Berries Black 201210 0.8

After attempting `SUMIFS`

as suggested below, it appears that I would need to specify each criteria (Category, Group, YearMon) for each data set to do the comparison. Perhaps there is some other functionality that would work?

**Update 2**

Another suggestion was to combine the data sets to calculate the difference from in a pivot. After combining these two data sets, I can’t seem to figure out a way to do the difference calculation without including another column that identifies each data set (1 or 2). From there I’m able to do the comparison. Am I doing something incorrectly?

**Update 3**

Following an answer below, I’ve simply negated the values of the second data set to get the difference.

To demonstrate what I was suggesting. Take your Data 1, multiply all amounts by -1, and add Data 2 to it, gives you:

Doing a pivot table based on that combined data gives you:

The pivot table SUM does the difference for you, for whatever row/column combinations you want. The trick is the multiplying one Data set by -1

*Trick: Type -1 in a new cell, Ctrl-C on that cell, then select all the values in Data 1 group, then do Paste Special and select ‘Multiply’*

### Answer：

This might help you:

```
=INDEX($B$11:$H$14,MATCH($A18,$A$11:$A$14,0),MATCH(B$17,$B$10:$H$10,0))-(INDEX($B$3:$H$7,MATCH($A18,$A$3:$A$7,0),MATCH(B$17,$B$2:$H$2,0)))
```

I don’t think there are any built in functions to combine pivot tables. So generally the issue does not seem to be specific to pivot tables – but rather just how to combine/subtract/add two seperate tables of data.

Not sure if you were looking for a VBA solution, but that is one way to do it with an (extremely verbose) spreadsheet formula. You could also probably use SUMIF formulas like Scott mentioned or just work with the orginal data. Good Luck.

Tags: excelexcel