"R1C1:last row of the document" (no matter if the rows change, the macro should still work?)

Fra88

New Member
Joined
Jan 17, 2017
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hi All :)

I have a macro creating a Pivot from a database (Hello)
The macro is working.

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Hello!R1C1:R278C52", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion15

My problem is that the database "Hello" is created each week, and it has different data inside.

This macro was created by recording, and was set with these data : R1C1:R278C52
Therefore, the macro doesn't work if the workbook has different rows..

Instead of manually going and changing data, is there a way to substitute "R1C1:R278C52" with "R1C1:whatever the last row of this document is"?

Thank you :) !!

Fra
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You can use a variableto hold the last row
Code:
LR = ThisWorkbook.Sheets(WS).Cells(Rows.Count, 1).End(xlUp).Row

You can then use the variable when referring to ranges
 
Upvote 0
I would use:

Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Hello!" & sheets("Hello").Range("A1").Currentregion.address(referencestyle:=xlR1C1), Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion15
 
Upvote 0
Thank you both :) !

I tried your solution RoryA, but it doesn't work :(
May Excel 2013 affect it?

About the first one, Scott could you help me implementing it?

I inserted

Dim LR As Long

LR = Cells(Rows.Count, 1).End(xlUp).Row


ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Hello!R1C1:R" & LR & "C23", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion15


'Worksheets(Worksheets.Count).Select
'Worksheets(Worksheets.Count).Activate
Sheets("Sheet2").Select
Cells(3, 1).Select

Sheets("DataToComment").Activate

ActiveSheet.PivotTables("PivotTable1").PivotFields("COUNTRY").Orientation = xlRowField
ActiveSheet.PivotTables("PivotTable1").PivotFields("COUNTRY").Position = 1



And it stucks at this line: ActiveSheet.PivotTables("PivotTable1").PivotFields("COUNTRY").Orientation = xlRowField
But probably I am doing it wrong

Thanks
 
Upvote 0
Sorry Rory, I had put an extra space

It works :D!
 
Upvote 0
No problem - glad to help. That version will also work if you happen to add new columns.
 
Upvote 0
I have not used R1C1 notation so I am not sure but maybe?

Code:
[COLOR=#008000]"Hello!R1C1:R"& LR &"C52",[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,214,853
Messages
6,121,935
Members
449,056
Latest member
denissimo

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