(Solved) – Creating pivot in excel using vba fails when there are more data


My Excel:-

enter image description here

My VBA Code to create Pivot:-

Sub Macro10()
' Macro10 Macro
' Keyboard Shortcut: Ctrl q
    Application.CutCopyMode = False
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R20C4", Version:=6).CreatePivotTable TableDestination:= _
        "Sheet2!R3C1", TableName:="PivotTable3", DefaultVersion:=6
    Cells(3, 1).Select
End Sub

The above creates Pivot, but when there are more/fewer rows in the excel, it gives an error.

Leave a Reply

Your email address will not be published. Required fields are marked *