Range Name define and transfer within macro

Roncondor

Board Regular
Joined
Jun 3, 2011
Messages
67
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)
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,274
Office Version
  1. 365
Platform
  1. Windows
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?
 

Roncondor

Board Regular
Joined
Jun 3, 2011
Messages
67
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)
 

Roncondor

Board Regular
Joined
Jun 3, 2011
Messages
67
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)
 

Watch MrExcel Video

Forum statistics

Threads
1,127,853
Messages
5,627,263
Members
416,236
Latest member
Lynchbox

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
Top