Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to implement the pivot cache object in C#. The code is building fine, however on running the code I am getting value does not fall within the expected range error on the below line of code

C#
Excel.PivotTable oPivotTable = (Excel.PivotTable)oSheet.PivotTables().Add(PivotCache: oPivotCache, TableDestination: oRange2, TableName: "Summary");


Here oRange2 has been initiated as under


C#
Excel.Range oRange2 = (Excel.Range)oSheet1.Range["A3:G10"];


Can anyone please explain the meaning of these two lines and how are we to set the values, or rather, what does each value mean?
Posted
Updated 14-Sep-15 19:52pm
v2

1 solution

Hi,

Since I couldn't get much help on the net, I had no option but to debug it on my own, I realized there would be a ton of people who might face the same problem. Hence, m posting the solution that worked for me.

The second line of code
C#
Excel.Range oRange2 = (Excel.Range)oSheet1.Range["A3:G10"];


this sets the range of the pivot table in the worksheet, hence it can be better given as under

C#
Excel.Range oRange2 = (Excel.Range)oSheet1.Range["Sheet2!$A:$K"]; 


considering sheet 1 would have the data present.

Once I finished that and gave the pivot a definite number of columns it worked for me.

There is another way of going about it,

C#
Excel.PivotTable oPivotTable = oPivotCache.CreatePivotTable(oRange2);


This also gives no error in case your range defined is correct.

I hope this helps someone out there :)

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900