+ Reply to Thread
Results 1 to 4 of 4

VBA PivotTable Automatic Range-selection

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: VBA PivotTable Automatic Range-selection

    Hi..

    You need to add some code that will detect the bottom of your range..

    Lets say your data is from Column A:J.. starting in row 2.. but the number of rows can change..

    In this case.. you need to find the last used row in your data range..

    First Dimension your variable..

    Dim LastRow as Long
    then.. to find the Last Row (assuming you have data in Column A all the way till the bottom)..

    'Find Last Row of the Data Range
    LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Then... Your Pivot Table code can be..

     ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            Sheets("Sheet1").Range("A2:J" & LastRow), Version:=xlPivotTableVersion10).CreatePivotTable _
            TableDestination:=Sheets("Sheet2").Cells(2, 1), TableName:="PivotTable1", DefaultVersion _
            :=xlPivotTableVersion10
    Be careful using..
    DefaultVersion _
            :=xlPivotTableVersion10
    As that will limit the Excel versions your Pivot table will work in.. but if its only for your local machine.. thats fine..


    Just noticed you might need to find the last used column...

    This is the most reliable thing for that..

    Dim rLastCell As Range
    
    Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
    Last edited by apo; 06-20-2013 at 04:23 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1