Need to create Pivot table with macro and account for varying range sizes

kcleere

Active Member
Joined
Oct 4, 2006
Messages
314
Greetings,

I am able to create a pivot table with a recorded macro but I do not know how to edit the code so that it automatically adjusts to the data range size as it varies from one sheet to another. Below I'm posting the part of the manually recorded VBA that "I think" establishes the initial data range and I'm hoping someone can tell me how to edit it to work as needed. Thank you.



'Sub Pivot()
'
' Pivot Macro
'
' Keyboard Shortcut: Ctrl+w
'
' Sheets.Add
' ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
' "Export (9)!R1C1:R172C7", Version:=6).CreatePivotTable TableDestination:= _
' "Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion:=6
' Sheets("Sheet1").Select
' Cells(3, 1).Select
' With ActiveSheet.PivotTables("PivotTable1")
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You can use CurrentRegion.Address...

Code:
SourceData:=Worksheets("Export (9)").Range("A1").CurrentRegion.Address)

However, here's another way to create your pivot table...

Code:
[FONT=Courier New][COLOR=darkblue]Sub[/COLOR] Pivot()

    [COLOR=darkblue]Dim[/COLOR] PCache [COLOR=darkblue]As[/COLOR] PivotCache
    [COLOR=darkblue]Dim[/COLOR] PT [COLOR=darkblue]As[/COLOR] PivotTable
    
    [COLOR=green]'Create a pivot cache[/COLOR]
    [COLOR=darkblue]Set[/COLOR] PCache = ActiveWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase, _
        SourceData:=Worksheets("Export (9)").Range("A1").CurrentRegion.Address)
    
    [COLOR=green]'Add a new worksheet[/COLOR]
    Worksheets.Add
    
    [COLOR=green]'Create the pivot table from the cache[/COLOR]
    [COLOR=darkblue]Set[/COLOR] PT = ActiveSheet.PivotTables.Add( _
        PivotCache:=PCache, _
        TableDestination:=Range("A1"), _
        TableName:="PivotTable1")
        
    [COLOR=darkblue]With[/COLOR] PT
        [COLOR=green]'Add fields[/COLOR]
        '
        [COLOR=green]'[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
        
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]
 
Upvote 0
Thank you. Initially I was not able to make these work. I'll try again when I have more time.
 
Upvote 0
Are you getting an error? If so, where does the error occur? And what type of error do you get?
 
Upvote 0
Using your second option to create a pivot...this is the section that fails. It "could" be that it specifies a sheet (export) name and the sheet name can vary?

'Create a pivot cache
' Set PCache = ActiveWorkbook.PivotCaches.Create( _
' SourceType:=xlDatabase, _
' SourceData:=Worksheets("Export").Range("A1").CurrentRegion.Address)
'
'Add a new worksheet
 
Upvote 0
If you make sure that the sheet containing the source data is the active sheet, you can refer to the active sheet

Code:
[COLOR=#333333]SourceData:=ActiveSheet.Range("A1").CurrentRegion.Address[/COLOR]

Or, simply...

Code:
[COLOR=#333333]SourceData:=Range("A1").CurrentRegion.Address[/COLOR]

Of course, when the new worksheet is added, it becomes the active sheet.
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,050
Members
449,206
Latest member
Healthydogs

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