Consulting

Results 1 to 3 of 3

Thread: Need help with pivot table data range

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Posts
    82
    Location

    Need help with pivot table data range

    I recorded a macro to build a pivot table, and in the part of the code where it selects the data range, it is a specific number of rows. My reports will vary in the number of rows of data everytime they are run, so this is not going to work. The relevant code currently looks like this:

    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= "Sheet1!R1C1:R14457C7").CreatePivotTable TableDestination:="", TableName:= "PivotTable1", DefaultVersion:=xlPivotTableVersion10

    How can I change this to capture the entire range of data, however many rows it may be?

    Thank you.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim LastRow As Long

    With ActiveWorkbook

    With .Worksheets("Sheet1")

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    .PivotCaches.Add(SourceType:=xlDatabase, _
    SourceData:="Sheet1!R1C1:R" & LastRow & "C7").CreatePivotTable _
    TableDestination:="", _
    TableName:="PivotTable1", _
    DefaultVersion:=xlPivotTableVersion10
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Aug 2006
    Posts
    82
    Location
    That did it! Thanks!

Posting Permissions

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