This code explains how tocreate a pivot table using vba code in excel macros, use the following function to create the pivot
sub CreatePivot()
Dim pcache As PivotCache
Dim ptable As PivotTable
Dim ws As Worksheet
Set ws = ActiveSheet
Set pcache = ActiveWorkbook.PivotCaches.Create(xlDatabase, ws.UsedRange)
Set ptable = pcache.CreatePivotTable(ws.[d20], "Demo Pivot", True)
Call ptable.AddFields(ptable.PivotFields("Customer").Name, ptable.PivotFields("Item").Name)
Call ptable.AddDataField(ptable.PivotFields("Qty"), "Quantity", xlSum)
End Sub
Considering the following Excel Data
| Customer |
Item |
Qty |
| Ravi |
Apple |
10 |
| Rajesh |
Pears |
20 |
| Ravi |
Pears |
5 |
62ca8253-fedc-4a28-912a-610bdc550f90|0|.0
if you have created the Model without the key attribute you will probably ends with the following error:
public class StudentsModel
{
public int Studentid{get;set;}
[Required]
public string Name{get;set;}
public int Age{get;set;}
}
Error would be :

The solution to this issue is :
public class StudentsModel
{
[Key]
public int Studentid{get;set;}
[Required]
public string Name{get;set;}
public int Age{get;set;}
}
95dd6b61-7736-43d3-b656-e7981a0029f3|0|.0
Change the registery entry as per the following details
Config key: "use_mos_conf_livesite"
Value : 1
This is under compact section
c9db0d43-8661-46e7-9ab4-b6ab49fc7297|1|2.0