VBA - Named Range Using Cell References

MrSourApple

New Member
Joined
Oct 23, 2015
Messages
17
I need to create multiple named ranges for a workbook that is updated regularly. Each time the workbook is updated a new worksheet is added and it needs named ranges to make other formulas in the workbook continue to work.


  • "Admin" tab: This tab has the name of the new worksheet that will be created in cell E1 (since it changes regularly the creation of the worksheet is macro driven)


  • "Named Ranges" tab: This tab has the names that need to be used for the named ranges in cells B2:B29 (B2 is a named range for column B of the new tab, B3 another named range for column C of the new tab...)

So to try and summarize: I want to create named ranges on the new tab, the new tab name is referenced in Admin!E1. The names for the named ranges are listed in 'Named Ranges'!B2:B29.

Here is what I put together, please help (this is just for the first named range not for all):

Sub MakeNamedRanges()
Set UIIRng = Sheets(Sheets("Admin").Range("E1").Value).Range("B:B")
ActiveWorkbook.Names.Add _​
Name:=Sheets(Sheets("Named Ranges").Range("B2").Value), RefersTo:=UIIRng​
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I need to create multiple named ranges for a workbook that is updated regularly. Each time the workbook is updated a new worksheet is added and it needs named ranges to make other formulas in the workbook continue to work.


  • "Admin" tab: This tab has the name of the new worksheet that will be created in cell E1 (since it changes regularly the creation of the worksheet is macro driven)


  • "Named Ranges" tab: This tab has the names that need to be used for the named ranges in cells B2:B29 (B2 is a named range for column B of the new tab, B3 another named range for column C of the new tab...)

So to try and summarize: I want to create named ranges on the new tab, the new tab name is referenced in Admin!E1. The names for the named ranges are listed in 'Named Ranges'!B2:B29.

Here is what I put together, please help (this is just for the first named range not for all):

Sub MakeNamedRanges()
Set UIIRng = Sheets(Sheets("Admin").Range("E1").Value).Range("B:B")
ActiveWorkbook.Names.Add _​
Name:=Sheets(Sheets("Named Ranges").Range("B2").Value), RefersTo:=UIIRng​
End Sub

If you are naming entire columns as a 'Named Range', why not just refer to them as Columns("A"), Columns("B"), etc.? Perhaps I have misinterpreted the post.
 
Upvote 0
If you are naming entire columns as a 'Named Range', why not just refer to them as Columns("A"), Columns("B"), etc.? Perhaps I have misinterpreted the post.

I don't think I can do that because the ranges located on a different tab than where the names of the named ranges are. There are three tabs being used here:

(1) Admin - cell E1 (Admin!E1) has the name of the new tab
(2) Named Ranges - cells B2:B29 have the names of the ranges I want to use on the new tab
(3) NewTab - This tab is named using a macro, based on Admin!E1

In order to name the ranges on NewTab I need to tell it how to find the tab --- Sheets(Sheets("Admin").range("E1").Value
I add Range("B:B") to the end so it knows which column to choose. Up to this point I think my macro is working.

It seems like my macro is messing up when it gets to adding the names. Any ideas?
 
Upvote 0
You have 28 range names in your list, but it is unclear if you are trying to name 28 columns in a single sheet, or if you are trying to use 28 different names for the same column in 28 different sheets. You need to make your objective a little more understandable.
 
Upvote 0
I am trying name 28 columns in a single sheet - each column has a different name. The name of each 'named range' is being pulled from the sheet "Named Ranges". The tab these named ranges will apply to is identified as I mentioned earlier through the 'Admin' tab.
 
Upvote 0
See if this works
Code:
Sub rngNames()
Dim sh1 As Worksheet, Sh2 As Worksheet, i As Long, nm As String
Set sh1 = Sheets("Admin")
Set Sh2 = Sheets("Named Ranges")
nm = sh1.Range("E1").Value
    For i = 2 To 28
        Sheets(nm).Names.Add Sh2.Cells(i, 2).Value, Sheets(nm).Columns(i)
    Next
End Sub
 
Upvote 0
Thank you JLGWhiz, this worked just as it was supposed to (had to adjust "For i = 2 to 28" to be "2 to 29" because it was cutting off the last named range but it works great. I am not all that familiar with vba and this was a huge help.
 
Upvote 0
I actually just noticed this - is there a quick way to make all of the new named ranges have a workbook scope? I didn't realize creating them this way would make them have a scope of the worksheet.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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