dan_l
Active Member
I make about 30 or so dynamic ranges for charting purposes. The series are predictable. Like:
=OFFSET('BLG'!$C$7,0,0,1,COUNT('BLG'!$C$7:$X$7))
=OFFSET('BLG'!$C$8,0,0,1,COUNT('BLG'!$C$8:$X$8))
=OFFSET('BLG'!$C$9,0,0,1,COUNT('BLG'!$C$9:$X$9))
I could just do it by hand, but I wonder if there isn't a VBA solution that would just create all 30 DNR's for me. I found this:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=157
Sub NamedRange()
Dim Rng1 As Range
'Change the range of cells (A1:B15) to be the range of cells you want to define
Set Rng1 = Sheets("Sheet1").Range("A1:B15")
ActiveWorkbook.Names.Add Name:="MyRange", RefersTo:=Rng1
End Sub
Now I know if I use application.offset it will just return a range with the exact size as is currently on the sheet. That won't update until the macro refreshes (which isn't practical for this project). Typically, in a situation where I needed dynamic in a cell I wouldn't do cell.value = application.sum(whatever), I would do cell.value ="=sum(whatever)".
So could I do:
for ticker 1 to 10
set rng1 = "=OFFSET('BLG'!$C$"&ticker&",0,0,1,COUNT('BLG'!$C$"&ticker&":$X$"&ticker&"))"
ActiveWorkbook.Names.Add Name:="Range"&ticker, RefersTo:=Rng1
next ticker
Or am I crazy?
=OFFSET('BLG'!$C$7,0,0,1,COUNT('BLG'!$C$7:$X$7))
=OFFSET('BLG'!$C$8,0,0,1,COUNT('BLG'!$C$8:$X$8))
=OFFSET('BLG'!$C$9,0,0,1,COUNT('BLG'!$C$9:$X$9))
I could just do it by hand, but I wonder if there isn't a VBA solution that would just create all 30 DNR's for me. I found this:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=157
Sub NamedRange()
Dim Rng1 As Range
'Change the range of cells (A1:B15) to be the range of cells you want to define
Set Rng1 = Sheets("Sheet1").Range("A1:B15")
ActiveWorkbook.Names.Add Name:="MyRange", RefersTo:=Rng1
End Sub
Now I know if I use application.offset it will just return a range with the exact size as is currently on the sheet. That won't update until the macro refreshes (which isn't practical for this project). Typically, in a situation where I needed dynamic in a cell I wouldn't do cell.value = application.sum(whatever), I would do cell.value ="=sum(whatever)".
So could I do:
for ticker 1 to 10
set rng1 = "=OFFSET('BLG'!$C$"&ticker&",0,0,1,COUNT('BLG'!$C$"&ticker&":$X$"&ticker&"))"
ActiveWorkbook.Names.Add Name:="Range"&ticker, RefersTo:=Rng1
next ticker
Or am I crazy?