Specify Selection as Range in Pivot Table Macro

floridagunner

Board Regular
Joined
Jul 20, 2007
Messages
60
Hello all,

I want to specify the active selection as the range for my pivot table but I keep getting an error message. Funny thing is this recorded code always used to work before but for some reason its not working anymore. The only change I had made in the recorded code was changing source data to selection.

I'm using the code below

Code:
Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        Selection, Version:=xlPivotTableVersion14). _
        CreatePivotTable TableDestination:="", TableName:="PivotTable4", _
        DefaultVersion:=xlPivotTableVersion14
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    
    ActiveSheet.Move After:=Sheets(Worksheets.Count)
    ActiveSheet.Cells(3, 1).Select

Can anyone help me fix this code please ?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If you say something like this:

Code:
    Sh = ActiveSheet.Name
    Rng = selection.Address(ReferenceStyle:=xlR1C1)

Then the source data something like this:

Code:
SourceData:= Sh & "!" & Rng
 
Upvote 0
Hello Steve,

I used your code modification but it still bring back an error message. The message states that;

"This command requires at least two rows of source data. You cannot use the command on a selection in only one row".

This is how I applied your modification:

Code:
    Sh = ActiveSheet.Name
    Rng = Selection.Address(ReferenceStyle:=xlR1C1)
    
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        Sh & "!" & Rng, Version:=xlPivotTableVersion14). _
        CreatePivotTable TableDestination:="", TableName:="PivotTable4", _
        DefaultVersion:=xlPivotTableVersion14
 
Upvote 0
Sounds like the selection is the problem. What is the selection in the activesheet when the code is run?
 
Upvote 0
It selects the entire range just as needed A1:V66285. Thats why I dont understand the error message as I definitely have all 66,285 rows selected.

Maybe its a problem of syntax like qoutes or commas ?
 
Upvote 0
The following is the code highlighted in yellow once I press the debug button:

Code:
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        Sh & "!" & Rng, Version:=xlPivotTableVersion14). _
        CreatePivotTable TableDestination:="", TableName:="PivotTable4", _
        DefaultVersion:=xlPivotTableVersion14
 
Upvote 0
Ah ok. You need to do your declaration of Rng after you have selected the data you require. At present you have it before.
 
Upvote 0
So:

Code:
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Rng = Selection.Address(ReferenceStyle:=xlR1C1)
 
Upvote 0
I used the following modification:

Code:
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    
    Sh = ActiveSheet.Name
    Rng = Selection.Address(ReferenceStyle:=xlR1C1)
    
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        Sh & "!" & Rng, Version:=xlPivotTableVersion14). _
        CreatePivotTable TableDestination:="", TableName:="PivotTable4", _
        DefaultVersion:=xlPivotTableVersion14
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    
    ActiveSheet.Move After:=Sheets(Worksheets.Count)
    ActiveSheet.Cells(3, 1).Select
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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