VBA-code to name ranges

Albert 1

Active Member
Joined
Feb 22, 2002
Messages
393
C1 houses "Products". The vba-code:
ActiveWorkbook.Names.Add Name:=Cells(1, 3).Value, RefersToR1C1:="=Sheet1!R2C2:R2C6"
gives C2:C6 the name "Products".
To name the ranges C2:C6, D2:D6, …..K2:K6, I tried:

Dim k as integer
For k = 3 to 9
ActiveWorkbook.Names.Add Name:=Cells(1, k), RefersToR1C1:="=Sheet1!R2Ck:R6Ck"
Next k

but that doesn't work. The k in Sheet1!R2Ck:R6Ck is not accepted.
What can I do?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

bolo

Active Member
Joined
Mar 23, 2002
Messages
423
Try this:

Dim k As Integer
For k = 3 To 9
ActiveWorkbook.Names.Add Name:=Cells(1, k), RefersToR1C1:="=Sheet1!R2C" & k & ":R6C" & k
Next k

hope it works for you.
 

Forum statistics

Threads
1,143,925
Messages
5,721,562
Members
422,372
Latest member
RoyRodger

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