naming a pasted range in VBA

miconian

Well-known Member
Joined
Aug 18, 2004
Messages
769
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!

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
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
It varies in size. Although it is, for the forseeable future, always going to be the same number of columns.
 
Upvote 0
Are you posting this data into the same location every time? Then you can just select that cell, then:

Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

To specify the range?
 
Upvote 0
I'm not sure that would make a difference. Having just pasted to the cells in question, haven't I already selected them?
 
Upvote 0

Forum statistics

Threads
1,223,171
Messages
6,170,480
Members
452,332
Latest member
The2ndQuest

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top