VBA and Last Row for Pivot Table

mihand

New Member
Joined
Jun 2, 2020
Messages
7
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I create monthly reports that vary in the number of rows. What code that should be used in VBA to show the last row for a pivot table? Below is what is existing. It is looking for data in the area of; Access!$A$1:$AJ$19280. Again, each month the row number, 19280, changes. It goes from A to AJ.

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Access!R1C1:R19280C36", Version:=6).CreatePivotTable TableDestination:= _
"Sheet2!R3C1", TableName:="PivotTable1", DefaultVersion:=6
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Add these lines before your code:
VBA Code:
pvtSh = "Access"
LastA = Sheets(datash).Cells(Rows.Count, "A").End(xlUp).row
nwRange = datash & "!" & Sheets(datash).Range("A1:AJ1").Resize(LastA).Address(ReferenceStyle:=xlR1C1)
Then, rather than using
SourceData:= _
"Access!R1C1:R19280C36"; Version:=etc etc

use
Code:
SourceData:= _
nwRange; Version:=etc etc
Bye
 
Upvote 0
It did not work. This is what I edited to;

pvtSh = "Access"
LastA = Sheets(datash).Cells(Rows.Count, "A").End(xlUp).Row
nwRange = datash & "!" & Sheets(datash).Range("A1:AJ1").Resize(LastA).Address(ReferenceStyle:=xlR1C1)
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
nwRange;, Version:=6).CreatePivotTable TableDestination:= _
"Sheet2!R3C1", TableName:="PivotTable1", DefaultVersion:=6
 
Upvote 0
The first code did not produce an error in VBA. The second did.
 
Upvote 0
Sorry, I was in error!
Wrong: pvtSh = "Access"
Il should be:
VBA Code:
 datash = "Access"

Try again...
 
Upvote 0
It says there is a syntax error for this;

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
nwRange; Version:=6).CreatePivotTable TableDestination:= _
"Sheet2!R3C1", TableName:="PivotTable1", DefaultVersion:=6
 
Upvote 0
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
nwRange; Version:=6).CreatePivotTable TableDestination:= _
"Sheet2!R3C1", TableName:="PivotTable1", DefaultVersion:=6
The problem is with the semicolon in red; it had to be a Comma :(
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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