Greetings,
I am having trouble creating a macro that will create a PivotTable using range variables from a dataset, and insert it into a new worksheet. Any ideas what I am doing wrong with this code? Thank you:
I am having trouble creating a macro that will create a PivotTable using range variables from a dataset, and insert it into a new worksheet. Any ideas what I am doing wrong with this code? Thank you:
Code:
Sub CreatePivotTable()
Dim r1, r2, r3 As Range
Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Offset(4, -1).Select
Set r1 = ActiveCell
Cells(Rows.Count, 1).End(xlUp).Select
Set r2 = ActiveCell
Set r3 = Range(r1, r2)
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=r3, Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion14
End Sub