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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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
 
Upvote 0
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.
 
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

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