In the code below, I basically copy a region of data, paste it into another workbook, and create a pivot table.
I'm just not sure how to set up the source of the pivot table so that it covers the exact range that I pasted. TableDestination should ideally be a named range that is equivalent to exactly what I just pasted on Sheet1.
I guess I can do something with Selection, but was hoping to avoid Selection when possible for faster execution.
Also open to any other advice.
Thanks!
I'm just not sure how to set up the source of the pivot table so that it covers the exact range that I pasted. TableDestination should ideally be a named range that is equivalent to exactly what I just pasted on Sheet1.
I guess I can do something with Selection, but was hoping to avoid Selection when possible for faster execution.
Also open to any other advice.
Thanks!
Code:
Sub createEstimates(currentName As String, dataSource As Range)
Dim book As Workbook
Dim dataSheet As Worksheet
Dim pivotSheet As Worksheet
dataSource.AutoFilter Field:=12, Criteria1:=currentName
dataSource.CurrentRegion.SpecialCells(xlCellTypeVisible).Copy
Set book = Workbooks.Add
Sheets(2).Paste
Application.CutCopyMode = False
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet2!R1C1:R10761C37", Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion12