"SourceData" and "TableDestination" for PivotTable

linesite

New Member
Joined
Oct 10, 2009
Messages
41
I download data from a web site on weekly basis and break information down by branch location and employees assigned to each branch--

Data source will fluctuate depending on new hire employees and those that quit or get laid off.

I used macro recorder to help create this section of VBA to help automate the PivotTable process.

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= "Data!R1C1:R134C4"

Two worksheets exist in this workbook; my data source named "Data" and the new worksheet for PivotTable data that I create in the beginning of the macro named "PT" for the location of PivotTable.

The "R1C1:R134C4" of this code will not always represent the data source each time I download data from the web site. It may contain less rows than the previous week or more depending on how many people we hire. What code can I insert to replace this section of code to include this fluctuation of data rows?

Should I replace code for "TableDestination:="Sheet10!R3C1"" with "TableDestination:="PT!R3C1""?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi Linesite,

I have pretty much the same issue as well. I recorded a macro create a pivot table and then adjusted the code to look for last row.
'Find Last used row on Test
Lastrow = Sheets("Test").Range("C" & Rows.Count).End(xlUp).Row
Debug.Print Lastrow

'Which sheet is the data coming from
Sheets("Test").Select
Range("C33:J" & Lastrow).Select

'Add a new sheet and create the Pivot Table
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Test!R33C3:R" & Lastrow & "C10", Version:=xlPivotTableVersion10). _
CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
, DefaultVersion:=xlPivotTableVersion10
Sheets("Sheet1").Select

See if this will help you out?
 
Upvote 0
Hi Linesite,

Sorry for the above, here is my code within the correct tags.

Code:
'Find Last used row on Billable Copy Report Sheet
    Lastrow = Sheets("Billable Copy Report").Range("C" & Rows.Count).End(xlUp).Row
    Debug.Print Lastrow
        
'Which sheet is the data coming from
    Sheets("Billable Copy Report").Select
    Range("C33:J" & Lastrow).Select
    
'Add a new sheet and create the Pivot Table
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Billable Copy Report!R33C3:R" & Lastrow & "C10", Version:=xlPivotTableVersion10). _
        CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
        , DefaultVersion:=xlPivotTableVersion10
    Sheets("Sheet1").Select
 
Upvote 0
Hey bigroo--

"ActiveSheet.UsedRange" in "SourceData:=" works in my worksheet. This code creates the PivotTable.

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=ActiveSheet.UsedRange). _
CreatePivotTable TableDestination:="", TableName:="PT", DefaultVersion:=xlPivotTableVersion11
 
Upvote 0

Forum statistics

Threads
1,223,943
Messages
6,175,551
Members
452,652
Latest member
eduedu

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