Trying to use a variable to name a single cell VBA

sparky_n_ames

New Member
Joined
May 20, 2010
Messages
15
I am using this string of code below to name a cell by converting variable Cmt1(Long) into a string variable cellname. I cannot get the line of code below to run. It works fine if I assign a string in "" marks to the variable. I need the variable cellname to match the contents of Variable Cmt1, which in theory it does as written. This is all part of a much longer string of code which runs just fine. I am just an entry level VBA user. I understand objects, properties, variables, etc. I just can't quite understand why this won't work. Any help is appreciated. Thanks.

Dim Cmt1 As Long
Dim celln As Range
Dim cellname As String


cellname = CStr(Cmt1)
'cellname = "FixedStringName" <--This works, but not the line above.
ActiveWorkbook.Names.Add Name:=cellname, RefersTo:=ActiveCell
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Cool blue

Board Regular
Joined
Dec 1, 2013
Messages
199
  1. Names that do not start with and underscore or a letter (including names that include only numbers) are not allowed.
  2. What is the value of Cmt1 when you convert it to a string and load it into cellname?
 
Upvote 0

sparky_n_ames

New Member
Joined
May 20, 2010
Messages
15
Cool Blue-

Thanks. I had forgotten that you can't use numbers to begin a named range.
Cmt1 equaled a number which increased by +1 each cycle of the loop. I added

cellname = "Comment #" & CStr(Cmt1)

This simple correction made it work.

Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,195,680
Messages
6,011,122
Members
441,586
Latest member
rodsin76

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