Using VBA to create range names

ElvisSteel

Board Regular
Joined
Mar 23, 2009
Messages
122
Hi,

Been a while since I was here but my memory is of a very knowledgeable forum so hopefully someone can help!

I have a workbook in which I need to create a set of range names that effectively repeat 100 times.
By way of explanation, say:
Cells A1:A4 need to be called "Item_001_Header"
Cells A17:B20 need to be called "Item_001_Codes"
Cells A55:B60 need to be called "Item_001_Data"

Then C1:C4 = "Item_002_Header", C17:C20 = "Item_002_Codes"
etc etc

Rather than sit and type in 300 ranges by hand I am trying to do this using VBA.
The idea is that I loop around the 100 sets, using Add Name to create the ranges but I am having all sorts of problems with the R1C1 format.

My code is as follows (so far):

Code:
 For p = 1 To 100    rn = "Item_" & WorksheetFunction.Text(p, "000") & "_Header"
    ActiveWorkbook.Names.Add Name:=rn, RefersToR1C1:= _
        "='Sheet One'!R1C1:R4C1"
    
    rn = "Item_" & WorksheetFunction.Text(p, "000") & "_Codes"
    ActiveWorkbook.Names.Add Name:=rn, RefersToR1C1:= _
        "=#Sheet One'!R17C21:R20C2"
 Next p

However this is plainly wrong as I need to the name references to be relative but I cannot get it to work using the usual R[x]C[y] format.

Anyone able to point out my mistake(s)

Thanks
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hello, maybe so:
Code:
Sub test()
    Dim iCl%
    iCl = 1
    For p = 1 To 100
        rn = "Item_" & WorksheetFunction.Text(p, "000") & "_Header"
        ActiveWorkbook.Names.Add Name:=rn, RefersToR1C1:= _
                                 "='Sheet One'!R1C" & iCl & ":R4C" & iCl
        rn = "Item_" & WorksheetFunction.Text(p, "000") & "_Codes"
        ActiveWorkbook.Names.Add Name:=rn, RefersToR1C1:= _
                                 "='Sheet One'!R17C" & iCl & ":R20C" & iCl
        iCl = iCl + 2
    Next p
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,362
Messages
6,124,502
Members
449,166
Latest member
hokjock

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