Variable Name Range Reference (VBA)

excells

New Member
Joined
Feb 6, 2014
Messages
29
Hi,
I'm writing code to name ranges and the actual name varies. Im using the following expression but its not working. Below "name" is a variable. Can somebody help me with this?

ActiveWorkbook.Names.Add name:="cstr(name)", RefersTo:=Selection




Thanks for your help in advance.

Juan
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
What exactly is "not working"? What error message do you get? Since "name" is a VBA object and a property you might want to name your variable something other than "name".
 
Upvote 0
Hi JonMo1,
thanks for prompt response.
I get Run-time error '1004':
The name you entered is not valid


The full code is:

Sub Name_ranges()
x = ActiveCell.Row
Dim column As Integer
Dim name As String

column = 2


ActiveCell.Offset(0, 1).Select
Application.CutCopyMode = False
name = Selection.Copy
ActiveCell.Offset(0, -1).Select
Application.Run "'140206_JUAN_MACRO_MODULES.xlsm'!Select_Group_cells"
Application.CutCopyMode = False
'ActiveWorkbook.Names.Add name:="name", RefersTo:=Selection
' the above works but "name" is taken as a constant and the range is called
'name, as opposed to being called the value of "name",
ActiveWorkbook.Names.Add name:="cstr(name)", RefersTo:=Selection
'The above give me Run-time error'1004'

'End Sub


End Sub
 
Last edited:
Upvote 0
Hi Jonmo1,
I took the quotation marks and still get the Run-time error '1004.

Thanks for helping.

Juan
 
Upvote 0
Jonmo1 picked up the likely problem - see his post, but I still recommend you name the variable something other than "name".
 
Upvote 0
Hi Joemo,
I used the following
ActiveWorkbook.Names.Add name:=CStr(VarName), RefersTo:=Selection

still get the same Run Time error 1004.

any ideas?
cheers

Juan
 
Upvote 0
Hi Joemo,
I used the following
ActiveWorkbook.Names.Add name:=CStr(VarName), RefersTo:=Selection

still get the same Run Time error 1004.

any ideas?
cheers

Juan

What is the value of varName when you get the error?
 
Upvote 0
Hi JoeMo;
I hovered over the variable and found that the value was TRUE. This is a bit strange to me because the variable should be equal to another string picked by the program on a previous step.

What do you think?

Regards,
Juan
 
Upvote 0
Hi JoeMo;
I hovered over the variable and found that the value was TRUE. This is a bit strange to me because the variable should be equal to another string picked by the program on a previous step.

What do you think?

Regards,
Juan

I don't find anything in your earlier posts showing where you set the value of that variable. Can you post the entire code and indicate which line causes the error?
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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