Pivot table through VBA - Syntax issue

plix

Board Regular
Joined
Oct 10, 2006
Messages
187
Hello,

In the code below, the pre-defined range "ActRange" seems to be creating a problem:

<font face=Courier New><SPAN style="color:#007F00">' Find the LAST row</SPAN><br>LastRow = ActiveSheet.Cells.find(What:="*", _<br>SearchDirection:=xlPrevious, _<br>SearchOrder:=xlByRows).Row<br><br><SPAN style="color:#007F00">' Find the LAST column</SPAN><br>LastCol = ActiveSheet.Cells.find(What:="*", _<br>SearchDirection:=xlPrevious, _<br>SearchOrder:=xlByColumns).Column<br><br><SPAN style="color:#00007F">Dim</SPAN> MyWS <SPAN style="color:#00007F">As</SPAN> Worksheet<br><SPAN style="color:#00007F">Dim</SPAN> ActRange <SPAN style="color:#00007F">As</SPAN> Range<br><br><SPAN style="color:#00007F">Set</SPAN> MyWS = ActiveSheet<br><SPAN style="color:#00007F">Set</SPAN> ActRange = MyWS.Range(Cells(1, 1), _<br>    Cells(LastRow, LastCol))<br><br><SPAN style="color:#007F00">'Create pivot table 3</SPAN><br>ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _<br>        "'" & ActiveSheet.Name & "'" & ActRange).CreatePivotTable TableDestination:="", _<br>        TableName:="PivotTable3"</FONT>
...

Seems there must be something wrong with the syntax but cannot find what it is.
Thanks for any help,

Plix
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,374
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
ActRange is a range, not an addresss string. Use:
Rich (BB code):
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "'" & ActiveSheet.Name & "'" & ActRange
.Address(referencestyle:=xlR1C1).CreatePivotTable TableDestination:="", _
        TableName:="PivotTable3"
 
Upvote 0

plix

Board Regular
Joined
Oct 10, 2006
Messages
187
Rorya,

The following gave me an "invalid procedure call or argument" error:

<font face=Courier New><SPAN style="color:#007F00">' Find the LAST row</SPAN><br>LastRow = ActiveSheet.Cells.find(What:="*", _<br>SearchDirection:=xlPrevious, _<br>SearchOrder:=xlByRows).Row<br><br><SPAN style="color:#007F00">' Find the LAST column</SPAN><br>LastCol = ActiveSheet.Cells.find(What:="*", _<br>SearchDirection:=xlPrevious, _<br>SearchOrder:=xlByColumns).Column<br><br><SPAN style="color:#00007F">Dim</SPAN> MyWS <SPAN style="color:#00007F">As</SPAN> Worksheet<br><SPAN style="color:#00007F">Dim</SPAN> ActRange <SPAN style="color:#00007F">As</SPAN> Range<br><br><SPAN style="color:#00007F">Set</SPAN> MyWS = ActiveSheet<br><SPAN style="color:#00007F">Set</SPAN> ActRange = MyWS.Range(Cells(1, 1), _<br>    Cells(LastRow, LastCol))<br><br><SPAN style="color:#007F00">'Create pivot table 3</SPAN><br>ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _<br>        "'" & ActiveSheet.Name & "'" & ActRange.Address(ReferenceStyle:=xlR1C1)).CreatePivotTable TableDestination:="", _<br>        TableName:="PivotTable3"</FONT>
...

Any idea?

Thanks again,

Plix
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,374
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Sorry, missed out the exclamation mark:
Rich (BB code):
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "'" & ActiveSheet.Name & "'!" & ActRange.Address(ReferenceStyle:=xlR1C1)).CreatePivotTable TableDestination:="", _
        TableName:="PivotTable3"
 
Upvote 0

Forum statistics

Threads
1,190,948
Messages
5,983,808
Members
439,862
Latest member
FaisalAlTawil

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