VBA Coding for adding Pivot Table with changing data table

darmst24

New Member
Joined
Oct 17, 2024
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hello there,
Looking for some guidance on how to code into my macro adding a Pivot Table that has a variable data set.

I have the attached coding but does not work if the data set increases/decreases.

TIA!

Sub BuyerItemPivotTable()
'
' BuyerItemPivotTable Macro
'

'

VBA Code:
    Range("B1:C1").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "NReport!R1C2:R8431C3", Version:=6).CreatePivotTable TableDestination:= _
        "Pivot Tables!R7C2", TableName:="PivotTable", DefaultVersion:=6
    Sheets("Pivot Tables").Select
    Cells(7, 2).Select
    With ActiveSheet.PivotTables("PivotTable").PivotFields("Buyer Name")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable").AddDataField ActiveSheet.PivotTables( _
        "PivotTable").PivotFields("Item Code"), "Count of Item Code", xlCount
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hello,
Could you try to change the static reference in the source of the pivottable to the one of the selection, ie. replace :
VBA Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "NReport!R1C2:R8431C3", Version:=6).CreatePivotTable TableDestination:= _
        "Pivot Tables!R7C2", TableName:="PivotTable", DefaultVersion:=6


With
VBA Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        Selection.Address, Version:=6).CreatePivotTable TableDestination:= _
        "Pivot Tables!R7C2", TableName:="PivotTable", DefaultVersion:=6
 
Upvote 0
It is faster to just find the last row and store it in a variable instead of "Select"-ing the cells. The below code will look for the last row with data in column 3 and return that row number in "LastRow". Then it uses that variable to form "PRange" which is then referenced in the Pivot creation. Just an alternate solution.

VBA Code:
Dim LastRow as Long
Dim PRange as Variant

LastRow = Worksheets("NReport").Cells(Rows.Count, 3).End(xlUp).Row
PRange = "NReport!R1C2:R" & LastRow & "C3"

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        PRange, Version:=6).CreatePivotTable TableDestination:= _
        "Pivot Tables!R7C2", TableName:="PivotTable", DefaultVersion:=6
 
Upvote 0
Hello,
Could you try to change the static reference in the source of the pivottable to the one of the selection, ie. replace :
VBA Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "NReport!R1C2:R8431C3", Version:=6).CreatePivotTable TableDestination:= _
        "Pivot Tables!R7C2", TableName:="PivotTable", DefaultVersion:=6


With
VBA Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        Selection.Address, Version:=6).CreatePivotTable TableDestination:= _
        "Pivot Tables!R7C2", TableName:="PivotTable", DefaultVersion:=6

Hi there, thanks for your help firstly.

I replaced coding as suggested but I am getting a debug - invalid argument :unsure:

1729238498401.png
 
Upvote 0
Hello again,

My mistake, you need to specify the sheet on which the range is, which is not done by default by the Address property. Also since you are using R1C1 style i would correct like this :

Selection.Address(,,xlR1C1,True)

VBA Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Selection.Address(,,xlR1C1,True), Version:=6).CreatePivotTable TableDestination:= _
"Pivot Tables!R7C2", TableName:="PivotTable", DefaultVersion:=6


I have to point out the answer of NateSC is better VBA coding practice. But as i can tell you used the macro recorder, and in order to minimize the code modification, i did not corrected it. Also the online help PivotCaches.Create method (Excel) | Microsoft Learn recommands to use the range address instead of the range object for the SourceData argument.
 
Upvote 0
Hello again,

My mistake, you need to specify the sheet on which the range is, which is not done by default by the Address property. Also since you are using R1C1 style i would correct like this :

Selection.Address(,,xlR1C1,True)

VBA Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Selection.Address(,,xlR1C1,True), Version:=6).CreatePivotTable TableDestination:= _
"Pivot Tables!R7C2", TableName:="PivotTable", DefaultVersion:=6


I have to point out the answer of NateSC is better VBA coding practice. But as i can tell you used the macro recorder, and in order to minimize the code modification, i did not corrected it. Also the online help PivotCaches.Create method (Excel) | Microsoft Learn recommands to use the range address instead of the range object for the SourceData argument.
Ok thank you, I'm struggling to get this code to work so i'll try the other coding. Many thanks
 
Upvote 0
It is faster to just find the last row and store it in a variable instead of "Select"-ing the cells. The below code will look for the last row with data in column 3 and return that row number in "LastRow". Then it uses that variable to form "PRange" which is then referenced in the Pivot creation. Just an alternate solution.

VBA Code:
Dim LastRow as Long
Dim PRange as Variant

LastRow = Worksheets("NReport").Cells(Rows.Count, 3).End(xlUp).Row
PRange = "NReport!R1C2:R" & LastRow & "C3"

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        PRange, Version:=6).CreatePivotTable TableDestination:= _
        "Pivot Tables!R7C2", TableName:="PivotTable", DefaultVersion:=6
Hi There, thanks for your help!

I have tried your coding but I am getting the following error any suggestions?

1729249712257.png
 
Upvote 0

Forum statistics

Threads
1,223,915
Messages
6,175,354
Members
452,638
Latest member
Oluwabukunmi

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