Consulting

Results 1 to 2 of 2

Thread: Selecting Pivot Table Range with Dynamic Data

  1. #1

    Selecting Pivot Table Range with Dynamic Data

    Hi y'all. Completely new to the world of VBA, so I apologize for my utter ignorance. Three hours of googling hasn't brought me any closer to a solution, so I'm asking here. I'm trying to make a pivot table for dynamic data ranges. Found the below code which works great if you tell it the range, but can't figure out how to select a range from A1:last cell used instead of R100. Tried all sorts of things with SpecialCells(xlCellTypeLastCell) and a number of other suggestions online, but I can't for the life of me figure out how to work any of them into the code. At this point I wouldn't even mind a solution with no explanation, but the latter would be appreciated as well. Thanks!

    Sub CreatePivotTable()
    'PURPOSE: Creates a brand new Pivot table on a new worksheet from data in the ActiveSheet
    'Source: www.TheSpreadsheetGuru.com


    Dim sht As Worksheet
    Dim pvtCache As PivotCache
    Dim pvt As PivotTable
    Dim StartPvt As String
    Dim SrcData As String


    'Determine the data range you want to pivot
    SrcData = ActiveSheet.Name & "!" & Range("A1:R100").Address(ReferenceStyle:=xlR1C1)


    'Create a new worksheet
    Set sht = Sheets.Add


    'Where do you want Pivot Table to start?
    StartPvt = sht.Name & "!" & sht.Range("A3").Address(ReferenceStyle:=xlR1C1)


    'Create Pivot Cache from Source Data
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=SrcData)


    'Create Pivot table from Pivot Cache
    Set pvt = pvtCache.CreatePivotTable( _
    TableDestination:=StartPvt, _
    TableName:="PivotTable1")


    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Some simplifications


    Option Explicit
    
    
    Sub CreatePivotTable()
    'PURPOSE: Creates a brand new Pivot table on a new worksheet from data in the ActiveSheet
    'Source: www.TheSpreadsheetGuru.com
    
    
        Dim sht As Worksheet
        Dim pvtCache As PivotCache
        Dim pvt As PivotTable
        Dim StartPvt As Range
        Dim SrcData As Range
        
        
        'Determine the data range you want to pivot
        Set SrcData = ActiveSheet.Cells(1, 1).CurrentRegion
        
        
        'Create a new worksheet
        Set sht = Sheets.Add
        
        
        'Where do you want Pivot Table to start?
        Set StartPvt = sht.Range("A3")
        
        
        'Create Pivot Cache from Source Data
        Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase, _
        SourceData:=SrcData)
        
        
        'Create Pivot table from Pivot Cache
        Set pvt = pvtCache.CreatePivotTable( _
        TableDestination:=StartPvt, _
        TableName:="PivotTable1")
    
    
    
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •