VBA Excel 2013 - Creating a pivot table using a dimensioned range

DrewB68

New Member
Joined
Aug 18, 2017
Messages
7
Hi all,

I have written a macro to create pivot tables in a new tab of an Excel 2013 file. The columns (A:AE) will remain static, but the number of rows will vary. For the data source, I would prefer to use a specified range based upon a dimensioned variable named EndRow, something along the lines of "Data Source!("A1:AE" & EndRow)", which works fine for vlookups, etc., but isn't working for pivot tables. I tried dimensioning the range, as well, but haven't been able to figure out the code segment that will run successfully. Note that the last command below ('ActiveWorkbook.PivotCaches.Create...) is the bloated code created by the recorder; I want to replace "Data Source!R1C1:R1048576C31" with a smaller pre-determined range.

Code:
    Dim EndRow As Long
    EndRow = Range("F1").End(xlDown).Row
    Dim PvtSrc As Variant
    PvtSrc = Range("A1:AE" & EndRow)
    Sheets("Sheet1").Name = "Data Source"
    Sheets.Add After:=ActiveSheet
    Sheets("Sheet1").Name = "Pivots"
    
[COLOR=#66CC66][/COLOR]    'Create Pivot 1[COLOR=#66CC66]
[/COLOR]    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Data Source!("R1C1:R" & EndRow & "C31"), Version:=xlPivotTableVersion15). _
        CreatePivotTable TableDestination:="Pivots!R5C2", TableName:="PivotTable1" _
        , DefaultVersion:=xlPivotTableVersion15

    'ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Data Source!R1C1:R1048576C31", Version:=xlPivotTableVersion15). _
        CreatePivotTable TableDestination:="Pivots!R5C2", TableName:="PivotTable1" _
        , DefaultVersion:=xlPivotTableVersion15

Thanks for your help!
 

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.
EndRow = Cells(Rows.Count, 1).End(xlUp).Row
SourceData:= "Data Source!A1:AE" & EndRow

When you make from your table an excel-table (format as table) then you can use a dynamic rangename:
SourceData:= "Table1"
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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