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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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
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
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,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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