Macro to Create Pivot Table – Adjustable Range

Michael151

Board Regular
Joined
Sep 20, 2010
Messages
247
Hello all,

Wondering if anyone could help with the range function of using VBA to create a pivot table. The current range in the code below is "Raw Data!R1C1:R39000C33":

Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "Raw Data!R1C1:R39000C33", Version:=xlPivotTableVersion12).CreatePivotTable _
    TableDestination:="", TableName:="PivotTable5", DefaultVersion _
    :=xlPivotTableVersion12


What I’d like to do is have the macro find the range itself based on a specific column. In my raw data, I’m using the column marked “deal_id” (row 1 header), to find the range. The last row of data based on this column is the last row used to make the pivot table. The macro would be something like this:

SourceData:=
“Raw Data!R1C1 and last row and C33

Last row = look in row 1 header, find “deal_id”, find last row with data in this column

Create Pivot Table with this range

Any help is most appreciated, thanks!
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Michael151

Board Regular
Joined
Sep 20, 2010
Messages
247
Something like:

FindCol = Rows(1).Find("deal_id", LookIn:=xlValues, LookAt:=xlWhole).Column

With Column, find last row

Range = R1C1 through last row and C33
 

Michael151

Board Regular
Joined
Sep 20, 2010
Messages
247
Here's what I've worked up so far, but can't seem to get it to work. Not sure about the Range function - how to define that it starts at R1C1, ends at Column 33, and ends at the last row with data under the heading "deal_id":


Code:
Dim cFind As Long
Dim lr As Long, Rng As Range

cFind = Cells.Find("deal_id", LookIn:=xlValues, LookAt:=xlWhole).Column
lr = Cells(Rows.Count, cFind).End(xlUp).Row
Rng = Range(Cells(2, cFind), Cells(Rows.Count, lr))
    
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    Rng, Version:=xlPivotTableVersion12).CreatePivotTable _
    TableDestination:="", TableName:="PivotTable5", DefaultVersion _
    :=xlPivotTableVersion12
 

Watch MrExcel Video

Forum statistics

Threads
1,127,557
Messages
5,625,501
Members
416,114
Latest member
Ayush_123

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
Top