Create and use a variable named range to generate a pivot table/report

I have data I generate from db query that outputs as a .xls file.  See attached example.

I'd like to write a macro that will select the data block, name it "data", then use that named range to generate a pivot table.

The data in the "data" range will vary by the number of rows with each unique db query that I run.

I've tried recording a macro using relative references as follows, but that always seems to create a defined range
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    ActiveWorkbook.Names.Add Name:="data", RefersToR1C1:="=QRESULT!R1C1:R19C8"

How do write a macro that will select A1 through (xlLastCell) and name it "data"?
mycomacAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
No attachment.
mycomacAuthor Commented:
Sorry about that. Here it is.
Book2.xlsx
byundtMechanical EngineerCommented:
If you don't need to refresh the PivotTable, you could create it using a range like:
Dim rg As Range
Set rg = Range("A1").CurrentRegion
ActiveWorkbook.Names.Add Name:="data", RefersToR1C1:="='" & rg.Worksheet.Name & "'!" & rg.Address(ReferenceStyle:= xlR1C1)

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
The following code will create a Named Range "Data" depending the data on the sheet QRESULT assuming row1 being the header row.

Sub CreateNamedRange()
Dim ws As Worksheet
Dim lr As Long, lc As Long
Set ws = Sheets("QRESULT")
lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
lc = ws.Cells(1, Columns.Count).End(xlToLeft).Column
ws.Range(ws.Cells(1, "A"), ws.Cells(lr, lc)).Name = "Data"
End Sub

Open in new window

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Okay that can also be written as below....

Sub CreateNamedRange()
Dim ws As Worksheet
Set ws = Sheets("QRESULT")
ws.Range("A1").CurrentRegion.Name = "Data"
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
byundtMechanical EngineerCommented:
And if you need a dynamic named range so your PivotTable can be refreshed after data are added or subtracted, then you could use:
Dim rg As Range
Dim wsName As String
Set rg = ActiveCell.CurrentRegion
wsName = "'" & rg.Worksheet.Name & "'"
ActiveWorkbook.Names.Add Name:="data", RefersToR1C1:= _
        "=" & wsName & "!R" & rg.Row & "C" & rg.Column & _
        ":INDEX(" & wsName & "!R" & rg.Row & "C" & (rg.Column + rg.Columns.Count - 1) & ":R" & ActiveSheet.Rows.Count & _
        "C" & (rg.Column + rg.Columns.Count - 1) & ",COUNTA(" & wsName & "!R" & rg.Row & "C" & rg.Column & ":R" & ActiveSheet.Rows.Count & "C" & rg.Column & "))"

Open in new window

mycomacAuthor Commented:
Thanks.  Good compact solution.
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome. Glad we could help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.