using variables in named ranges vba

novel24

New Member
Joined
Nov 18, 2009
Messages
49
Hi. The following code allows me to name a range, but I must manually choose the column # and the names for each range each time. I would like to find a way to automate this process so I can just input at the top the column number, and the name for each named range I desire.

Sub namedrange()
'
' namedrange Macro
'
'Creates a named range for specified time-series data. Note: data must
'be organized with dates in 'left-hand' column; values in right.

'I want the column number in 'Add Date' to be variable, and the name creations to
'variable.

'Add Dates to named range

ActiveWorkbook.Names.Add NAME:="Dates", RefersToR1C1:= _
"=OFFSET(Sheet1!R4C7,,,COUNTA(Sheet1!R4C7:R500C7),1)"
ActiveWorkbook.Names("Dates").Comment = ""


'Add Values to named range

ActiveWorkbook.Names.Add NAME:="Values", RefersToR1C1:= _
"=OFFSET(Dates,0,1)"
ActiveWorkbook.Names("Values").Comment = ""



End Sub
 

Some videos you may like

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

xld

Banned
Joined
Feb 8, 2003
Messages
5,378
Code:
Public Sub NameRange(col As Long, RangeNAme As String)

ActiveWorkbook.Names.Add NAME:=RangeName, RefersToR1C1:= _
"=OFFSET(Sheet1!R4C" & col & ",,,COUNTA(Sheet1!R4C" & col & ":R500C" & col & "),1)"
End Sub
 

novel24

New Member
Joined
Nov 18, 2009
Messages
49
Code:
Public Sub NameRange(col As Long, RangeNAme As String)

ActiveWorkbook.Names.Add NAME:=RangeName, RefersToR1C1:= _
"=OFFSET(Sheet1!R4C" & col & ",,,COUNTA(Sheet1!R4C" & col & ":R500C" & col & "),1)"
End Sub

This seems more like it -- how would I change the range name in this case (since it is linked through my code), thanks for the column part though.
 

novel24

New Member
Joined
Nov 18, 2009
Messages
49
Not sure yet. I probably would eventually create a message box of sorts that would ask for a name input and the column the data is located, but right now I'd rather just automate the process. I'm just looking to modify less code, so something that I only have to enter at the top of the code is fine.

Where do you want to put the column number and name? On a worksheet somewhere?
 

xld

Banned
Joined
Feb 8, 2003
Messages
5,378
This seems more like it -- how would I change the range name in this case (since it is linked through my code), thanks for the column part though.

You pass it as one of the procedure call parameters.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,492
Messages
5,601,994
Members
414,490
Latest member
Rip181

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