Name a range in VBA (should be easy)

WillR

Well-known Member
Joined
Feb 18, 2002
Messages
1,143
Ok... I have a query that refreshes into an excel worksheet and want to Name the output which will always be from cell A3 to cell D"something" (i.e. D 'end xldown??')

What would be the VB code to select the output and Name the whole datatable???

I'm sure this is an easy one.... for someone.

Thanks,
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You select the range that you want
range("a1:A2").select

and then use range.name = "name" to name the range.

To name the whole application (is this what you want?) you can use

Application.Caption = "Name"
 
Upvote 0
On 2002-10-30 07:40, k209310 wrote:
You select the range that you want
range("a1:A2").select

and then use range.name = "name" to name the range.

To name the whole application (is this what you want?) you can use

Application.Caption = "Name"

I think you missed my point. The range will potentially change each time the data is imported. I wish to, using VB, not manually, firstly determine the extent of the range (i.e. A3:Dwhatever) and then name it, as part of the data refresh procedure.

I know i can use the following code...

Code:
Names.Add Name:="Groups", RefersTo:="

But its the refers to bit i'm trying to ascertain, .....
 
Upvote 0
I often solve this with usedrange

SheetCodeName.usedrange

If I need to get around headers then try this :
SheetCodeName.usedrange.offset(1,1).Resize(sheetCodeNAme.usedrange.rows.count -1, SheetCodeName.usedrange.columns.count - 1)

If this range expands continously thats all you need, but usually it shrinks every now and then too. If so you'll need to .delete empty cells.
Perhaps even .EntireRow.delete to avoid selecting a lot of useless empty cells.
 
Last edited:
Upvote 0
Alternatively,

Range("A3:D" & Range("D3").End(xlDown).Row).Name = "DataTable"

Hey all,

This is exactly the thing I'm trying to do, except take a range which goes to the right instead of downwards.

I tried:

Code:
Range("C39:" & Range("C39").End(xlRight).Column & "39").Name = "REPS"

But came up with error 1004 (Application or object defined error)


Alternatively, I do have variables named in VBA for the number of rows across it will go if this is easier...


Cheers,
Cam
 
Upvote 0
Hey all,

This is exactly the thing I'm trying to do, except take a range which goes to the right instead of downwards.

I tried:

Code:
Range("C39:" & Range("C39").End(xlRight).Column & "39").Name = "REPS"

But came up with error 1004 (Application or object defined error)


Alternatively, I do have variables named in VBA for the number of rows across it will go if this is easier...


Cheers,
Cam


Just had a brainwave of how to do this... And it worked! For reference for anyone else the code I used selected the range and then proceeded to name it.

Here's the code:

Code:
Range("C39").Select
    Range(Selection, Selection.End(xlToRight)).Select
    
    Selection.Name = "REPS"
 
Upvote 0
Must you do this programmatically? You could manually add a dynamic named-range:-
Code:
=OFFSET(Sheet1!$A$3,0,0,COUNTA(Sheet1!$A$3:$A$1048576),4)
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

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