Range in Last column and Last Row in excel VBA for pivot

BMD44

Board Regular
Joined
Sep 25, 2019
Messages
72
Hi,

I am trying to pivot a table in excel.

The pivot works when the data starts from cell(1,1).

But when the placement of data cells change, the code does not create pivot.

Could you suggest the data range that needs to be changed for the code. Attached the file.

Data source starts from Row 7 and Col R (Cols R,S T,U)

Here is the script:

'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try
VBA Code:
LastRow = Cells(Rows.Count, "R").End(xlUp).Row
LastCol = Cells(7, Columns.Count).End(xlToLeft).Column
Set PRange = Cells(7, "R").Resize(LastRow - 6, LastCol - 17)
 
Upvote 0
Try
VBA Code:
LastRow = Cells(Rows.Count, "R").End(xlUp).Row
LastCol = Cells(7, Columns.Count).End(xlToLeft).Column
Set PRange = Cells(7, "R").Resize(LastRow - 6, LastCol - 17)
Thanks much for the quick response. This works.
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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