Macro to Name a Range that isn't pre-defined

Phoenix333

New Member
Joined
Sep 28, 2010
Messages
26
Hi Smart Peeplz!

I'm having problems with a bit of code I'm trying to use and I'm hoping someone out there can help.

I need the macro to create a named range based on a last column and row that can change. It won't change once the report is set up, but since this is a template that will be used for hundreds of projects, it needs to be flexible to accomodate a varying numbers of columns or rows

The starting cell will always be A10, but the ending cell can change depending on how many columns and rows are in the table.

Right now, the code I've created is

Sub NamePivotData()

Dim LCol As Long
Dim LRow As Long
Dim RngEnd As Long

LCol = Cells(10, Columns.Count).End(xlToLeft).Column
LRow = Cells(Rows.Count, 1).End(xlUp).Row
RngEnd = Cells(LRow, LCol)

ActiveWorkbook.Names.Add Name:="PivotData", _
RefersTo:="=$A$10:RngEnd"
End Sub

Sadly (and not unexpectedly), I end up with PivotData as a named range that refers to
='Calendar Setup'!$A$10:RngEnd

I need it to refer to ='Calendar Setup'!$A$10:$*$**
where * is the last column in the table and ** is the last row in the table.

As always, your help is GREATLY appreciated!
T-
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Got it! :biggrin:

I'm posting the answer in case someone else has this same issue.

Code:
Sub NamePivotData()
    
    Range("A10").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    ActiveWorkbook.Names.Add Name:="PivotData", RefersTo:=Selection
End Sub

Thanks!
T-
 
Upvote 0
Good job.

You can also do it without selecting:

Code:
Sub NamePivotData()
    ActiveWorkbook.Names.Add Name:="PivotData", _
                             RefersTo:=Range("A10", Range("A10").End(xlDown).End(xlToRight))
End Sub
 
Upvote 0
You're welcome, good luck.
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,395
Members
449,446
Latest member
CodeCybear

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