• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Dynamic Pivottable range

Hi Experts,

I am just trying make change the range of a Pivot table automatically. But somehow it's not working ?

Any help on this ?
Code:
  Wb1.Activate
  Sheets("Raw Data").Visible = True
  LstRow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
 
 
  Set Data_Sht = Wb1.Sheets("Raw Data")
  Set Pivot_Sht = Wb1.Sheets("Summary")
  Set StartPoint = Wb1.Sheets("Raw Data").Range("A1")
  Set Datarange = Data_Sht.Range("A1:K" & LstRow)
 
  'Set Pivottable name
  PivotName = "Pivottable8"
 
  NewRange = Data_Sht.Name & "!" & Datarange.Address
 
  'Change Pivot Table Data Source Range Address
  'Pivot_Sht.PivotTables(PivotName).ChangePivotCache _
  'Wb1.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange)
  With Sheet4
  .PivotTables("Pivottable8").ChangePivotCache ActiveWorkbook. _
  PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange _
  , Version:=xlPivotTableVersion14)
  .PivotTables("PivotTable8").PivotCache.Refresh
 
  Sheets("Raw Data").Visible = False
 
  End With
  end sub
 
Last edited by a moderator:
It's because you have space in sheet name "Raw Data".

Change your "NewRange" part of the code to...
Code:
NewRange = "'" & Data_Sht.Name & "'!" & DataRange.Address
 
Back
Top