+ Reply to Thread
Results 1 to 11 of 11

How to define a relative named range in VBA

Hybrid View

  1. #1
    John Broderick
    Guest

    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. #2
    Forum Contributor
    Join Date
    03-15-2005
    Location
    North Carolina
    MS-Off Ver
    2003 & 2007
    Posts
    180
    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

  3. #3
    Chip Pearson
    Guest

    Re: 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" <jbroderick@rfintegration.com> wrote in message
    news:O9ZxYhzBGHA.3292@TK2MSFTNGP09.phx.gbl...
    > 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. #4
    Bob Phillips
    Guest

    Re: 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" <jbroderick@rfintegration.com> wrote in message
    news:O9ZxYhzBGHA.3292@TK2MSFTNGP09.phx.gbl...
    > 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
    >
    >
    >
    >
    >




  5. #5
    John Broderick
    Guest

    Re: 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



  6. #6
    Bob Phillips
    Guest

    Re: 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" <jbroderick@rfintegration.com> wrote in message
    news:e6ZFsC0BGHA.3292@TK2MSFTNGP09.phx.gbl...
    > 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
    >
    >




  7. #7
    keepITcool
    Guest

    Re: 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


  8. #8
    Bob Phillips
    Guest

    Re: 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" <xrrcvgpbby@puryyb.ay> wrote in message
    news:xn0ebc3dm7j815n00akeepitcoolnl@news.microsoft.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




  9. #9
    Dave Peterson
    Guest

    Re: 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

  10. #10
    Dave Peterson
    Guest

    Re: 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

  11. #11
    Tim Williams
    Guest

    Re: 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" <jbroderick@rfintegration.com> wrote in message
    news:O9ZxYhzBGHA.3292@TK2MSFTNGP09.phx.gbl...
    > 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
    >
    >
    >
    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1