Naming Ranges VBA

kspence703

New Member
Joined
Oct 5, 2018
Messages
6
Hi, new to the site.

Am trying to name ranges through VBA. I have four columns. In row A would be the name I want to assign to the four different ranges I want the name to be assigned to all of column A and all of Column B etc. through Column D. For example..Cell A1 is titled "Unit and as such I want the range [column A] to be named "Unit" and of course would want to be able to repeat this for all other columns, changing of course based on the name in the first cell of the column.

I am able to name the first column with the following: Range("A2:A1000000").Name = "Unit"....however when I go and try and name the second column I receive a run-time error.

Let me know if anyone can help.
Thanks!


KS
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Dim Unit As Range
Set Unit = Thisworkbook.worksheets("Sheet1").Range("A2:A10000")

@KUYJS, that is not a named range, it is a variable.

@kspence703, what is the runtime error number and description? and can you post the full code you used to name both ranges?
 
Last edited:
Upvote 0
Along with what Mark has said, what are the names you want to use for cols B:D?
 
Upvote 0
You cannot have spaces in a named range
 
Upvote 0
If the first row contains the names you could use CreateNames.

Code:
Range("A1:D1000").CreateNames Top:=True

Note, this will create named ranges with the same no of rows and any spaces in the names will be replaced with underscores.

Also note, if you converted the data to a table you could use structured references to refer to the columns by their heading
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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