Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default How to define a relative named range in VBA

For example I want to create a name (CellBelow) that always refers to the
cell below the active cell.
I know how to do this with Insert Name / Define, but how does one do it
within VBA?

Thanks,
JB





  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default How to define a relative named range in VBA

Create a defined name that refers to =B1. Note that this differs
from typical defined names in that it uses relative rather than
absolute references (no dollar signs in the formula).


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"John Broderick" wrote in message
...
For example I want to create a name (CellBelow) that always
refers to the
cell below the active cell.
I know how to do this with Insert Name / Define, but how does
one do it
within VBA?

Thanks,
JB







  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How to define a relative named range in VBA

With ActiveCell
ActiveWorkbook.Names.Add Name:="CellBelow", _
RefersTo:="=" & .Offset(-.Row + 2, -.Column + 1).Address(False,
False, , True)
End With


--

HTH

RP
(remove nothere from the email address if mailing direct)


"John Broderick" wrote in message
...
For example I want to create a name (CellBelow) that always refers to the
cell below the active cell.
I know how to do this with Insert Name / Define, but how does one do it
within VBA?

Thanks,
JB







  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default How to define a relative named range in VBA

Thanks Bob,

this does the same thing as Insert Name /Define, which is the original
question I asked.

However I now realize when I use CellBelow on any sheet it always refers to
the original sheet that was active when I defined it.
I want CellBelow to refer to the cell below the active cell on the active
sheet, not the original sheet.

JB


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How to define a relative named range in VBA

John,

This does it but you need to do a Ctrl-Alt-F9 to get it to recalculate if
the cell below changes

With ActiveCell
ActiveWorkbook.Names.Add Name:="CellBelow", _
RefersToR1C1:="=!R[1]C[0]"
End With


--

HTH

RP
(remove nothere from the email address if mailing direct)


"John Broderick" wrote in message
...
Thanks Bob,

this does the same thing as Insert Name /Define, which is the original
question I asked.

However I now realize when I use CellBelow on any sheet it always refers

to
the original sheet that was active when I defined it.
I want CellBelow to refer to the cell below the active cell on the active
sheet, not the original sheet.

JB






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default How to define a relative named range in VBA

Bob,

Caveat:
When calculation is triggered from VBA code, these "global" relative
reference in a named function will give erroneous results as they will
point to the activesheet at the time of recalc.. dont ask me why,
...Jan Karel Pieterse once told me...


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Bob Phillips wrote :

John,

This does it but you need to do a Ctrl-Alt-F9 to get it to
recalculate if the cell below changes

With ActiveCell
ActiveWorkbook.Names.Add Name:="CellBelow", _
RefersToR1C1:="=!R[1]C[0]"
End With

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How to define a relative named range in VBA

Yeah, I think Charles Williams made that same point once.

It ain't perfect, but I couldn't see another way to get anywhere close to
the OP's request.

Bob


"keepITcool" wrote in message
.com...
Bob,

Caveat:
When calculation is triggered from VBA code, these "global" relative
reference in a named function will give erroneous results as they will
point to the activesheet at the time of recalc.. dont ask me why,
..Jan Karel Pieterse once told me...


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Bob Phillips wrote :

John,

This does it but you need to do a Ctrl-Alt-F9 to get it to
recalculate if the cell below changes

With ActiveCell
ActiveWorkbook.Names.Add Name:="CellBelow", _
RefersToR1C1:="=!R[1]C[0]"
End With



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default How to define a relative named range in VBA

How about:
Insert|Name|Define
Cellbelow
=INDIRECT("r[1]c",FALSE)



John Broderick wrote:

Thanks Bob,

this does the same thing as Insert Name /Define, which is the original
question I asked.

However I now realize when I use CellBelow on any sheet it always refers to
the original sheet that was active when I defined it.
I want CellBelow to refer to the cell below the active cell on the active
sheet, not the original sheet.

JB


--

Dave Peterson
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default How to define a relative named range in VBA

Or in code (missed that):

ActiveWorkbook.Names.Add Name:="CellBelow", _
RefersToR1C1:="=INDIRECT(""r[1]c"",FALSE)"

Dave Peterson wrote:

How about:
Insert|Name|Define
Cellbelow
=INDIRECT("r[1]c",FALSE)

John Broderick wrote:

Thanks Bob,

this does the same thing as Insert Name /Define, which is the original
question I asked.

However I now realize when I use CellBelow on any sheet it always refers to
the original sheet that was active when I defined it.
I want CellBelow to refer to the cell below the active cell on the active
sheet, not the original sheet.

JB


--

Dave Peterson


--

Dave Peterson
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default How to define a relative named range in VBA

You'd have to capture the selection change event in the sheet and then
redefine the range to point to the correct cell.

Eg:

ActiveCell.Offset(1,0).Name=RNGNAME

Where RNGNAME is a constant holding the name of your named range.


Tim.

--
Tim Williams
Palo Alto, CA


"John Broderick" wrote in message
...
For example I want to create a name (CellBelow) that always refers to the
cell below the active cell.
I know how to do this with Insert Name / Define, but how does one do it
within VBA?

Thanks,
JB









  #11   Report Post  
Posted to microsoft.public.excel.programming
jtp jtp is offline
external usenet poster
 
Posts: 1
Default How to define a relative named range in VBA


If I understand you correctly try this.

-Dim CellBelow as Range

set CellBelow = Activecell.offset(1,0)-

That should be all.



For example I want to create a name (CellBelow) that always refers to
the
cell below the active cell.
I know how to do this with Insert Name / Define, but how does one do
it
within VBA?

Thanks,
JB


--
jtp
------------------------------------------------------------------------
jtp's Profile: http://www.excelforum.com/member.php...o&userid=21132
View this thread: http://www.excelforum.com/showthread...hreadid=495614

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I have a cell value define a dynamic named range? CellShocked Excel Worksheet Functions 15 October 26th 11 02:08 AM
Named Range that uses "relative" range - Possible? Johnny_99 Excel Worksheet Functions 5 December 14th 09 08:39 AM
How to define a Named Range in VBA? Richard Excel Programming 3 December 12th 05 06:49 PM
Define a range based on another named range Basil Excel Worksheet Functions 2 February 21st 05 01:47 PM
relative reference in a macro to define a name Hal Forman Excel Programming 2 May 28th 04 07:13 PM


All times are GMT +1. The time now is 07:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"