Is there a way to change the range of a Pivot table using a macro, without having to use a table or named range?
I recorded a macro where I changed the range (see code below). But I was wondering if I could incorporate this statement - Range("A2", Range("A2").End(xlToRight).End(xlDown)).Select - into the source, so that it always selects the entire range of data regardless of how many rows there are?
I tried to replace the characters after Sheet1 in the code below with
Range("A2", Range("A2").End(xlToRight).End(xlDown)).Select
but that didn't work.
I'm sure it's possible to update the source automatically without using a table (there's too much data to use a table, and the spreadsheet slows down significantly when you update the range with a table).
VBA Code:
Sub change_pivot_source_data()
'
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"C:\Users\Documents\[Change Pivot Table source.xlsb]Sheet1!R1C1:R4C3", _
Version:=8)
End Sub