Range Name define and transfer within macro

Roncondor

Board Regular
Joined
Jun 3, 2011
Messages
79
hi - thank you - I always get great responses on Mr Excel

This is an easy question, but having trouble finding the solution

I want to name a range based on variables that have the Sheet name, Range that I want to define, Name that I want to assign

The code below does not work.

Also, what would the code be to duplicate a range name (not change) so that the oldname stays and the new names is also there

THANK YOU

Dim R As String
Dim NewName As String
Dim OldName As String
Dim TheSheet As String
Dim TheCells As String

NewName = "TheNewName"
TheSheet = "Sheet1"
TheCells = "$X$1:$Z$2"
R = "'" + TheSheet + "'!" & TheCells

ActiveWorkbook.Names.Add Name:=NewName, RefersTo:=Range(R)
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
It works fine for me, creating a named range called 'TheNewName' which refers to X1:Z2 on Sheet1 and has workbook scope.

How is it not working for you?
 
Upvote 0
Not sure - will look into it -thank you

Do you have code to duplicate a range name?
i.e. I want range name X to be named Y (in additon to X)
 
Upvote 0
Figured it out - but here is the answer in case will help someone else

Dim Oldname as string
Dim NewName as string
Dim A as string
Dim W as string
Dim R as string

Oldname ="OldName"
Newname = "Newname"

A = Range(OldName).Address
W = Range(OldName).Worksheet.Name
R = "'" + W + "'!" & A 'need the single quote in case the sheet name has spaces

ActiveWorkbook.Names.Add Name:=newname, RefersTo:=Range(R)
 
Upvote 0

Forum statistics

Threads
1,214,387
Messages
6,119,225
Members
448,877
Latest member
gb24

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