+ Reply to Thread
Results 1 to 9 of 9

Best way to name Dynamic ranges

Hybrid View

  1. #1
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Best way to name Dynamic ranges

    I've always used OFFSET like so
    keycolumn = OFFSET($A$2,,,COUNTA($A$2:$A$5000))
    Second column =OFFSET(keycolumn,,1)
    etc.
    The problem with that is blanks will screw up everything.

    I've seen on here (can't find the thread but I think it was Donkeyote
    EndRow =MATCH(9E+307, $A:$A, 1)
    Then
    keycolumn = INDEX($A:$A, 2):INDEX($A:$A, EndRow)
    Second Column =INDEX($B:$B, 2):INDEX($B:$B, EndRow)
    etc.
    Any disadvantages to using this technique?
    How about combining the two and developing the key column with method 2 and then using the OFFSET for other columns.

    Any other ways to set up this type of dynamic range?
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Best way to name Dynamic ranges

    You could replace your Offset() formula with:

    =OFFSET(Sheet1!$J$1,0,0,COUNTIF(Sheet1!$J:$J,"*?"),1)
    this will cut out the formula blanks at the bottom of the range (if any).
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Best way to name Dynamic ranges

    I think you're saying you need to determine the last row containing a numeric value and use that to determine your named range -- where interspersed amongst the values you may have blanks... ie using COUNT type approach will generate misleading ranges, correct ?

    One way is to use something along the lines of:

    Name: _test
    RefersTo: =OFFSET($A$2,0,0,MATCH(9.99999999999999E+307,$A:$A),1)

    This would account for blanks interspersed in the range, HOWEVER, would only work were the last value numeric... if you had text etc below the last number the above would ignore that.

    So you could in theory create the following:

    Name: BIGNUM
    RefersTo: 9.99999999999999E+307

    Name: BIGTXT
    Refers To: =REPT("z",255)

    Name: _lastno
    RefersTo: =LOOKUP(BIGNUM,CHOOSE({1,2},0,MATCH(BIGNUM,$A:$A)))

    Name: _lasttxt
    RefersTo: =LOOKUP(BIGNUM,CHOOSE({1,2},0,MATCH(BIGTXT,$A:$A)))

    Name: _lastlogic
    RefersTo: =LOOKUP(BIGNUM,CHOOSE({1,2},0,MATCH(TRUE,$A:$A)))


    You can then define your range using the MAX of the above:

    Name: _test
    RefersTo: =OFFSET($A2,0,0,MAX(_lastno, _lasttxt, _lastlogic),1)

    Does that help at all, not sure ?


    Someone else will have a better approach... I tend to avoid using DNR's myself wherever possible given they're invariably volatile -- that said using Named Constants for things like BIGNUM make a lot of sense :-)

  4. #4
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Best way to name Dynamic ranges

    ....looks a lot like a Domenic method...

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Best way to name Dynamic ranges

    Erm, no, in this instance I actually just put it together -- I've never done so previously but if this is something you've seen Domenic do before then that's all good... Domenic is more than aware as to just how highly I regard his XL techniques !

    I like to think that if I demonstrate a method that's to all intents & purposes the creation of another member otherwise not seen that I wouldn't try and pass it off as my own and would mention said member appropriately...

    The above still doesn't account for error values mind... should the last value be an error the range would ignore it ... though I'm sure there's a way to find the last error value also...
    Last edited by DonkeyOte; 02-11-2009 at 01:11 PM.

  6. #6
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Best way to name Dynamic ranges

    Wasn't meant to offend...just commented that the style (i.e.using all those Named Formulas is what I often see Domenic solutions consist of) ... that's all

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Best way to name Dynamic ranges

    OK, here's my tortured approach, similar to DO/Domenic's.

    Suppose we have a table of variable-length data starting with headers starting in D:F with a header in row 6. We want definitions that ensure that adding data to the table (top, bottom, or in between) extends all ranges appropriately to include it, and allows moving the table without hosing the ranges.

    Define:

    ptrTL Refers to: =$D$6
    rgnHdrDown Refers to: =ptrTL:$F$65536
    frmEnd Refers to: (any method appropriate to table contents; sometimes I just use the word End if I really want to have other data below the table)

    This defines everything from the header row downward. The actual data starts one row below that, so we define:

    tbl Refers to: =INDEX(rgnHdrDown, 2, 0):INDEX(rgnHdrDown, frmEnd, 0)

    This makes the table start one row below the header, and extend to the last row.

    Suppose the table contains values for x, y, z in columns D:F respectively. Then define

    ptrX Refers to: =$D$6
    ptrY Refers to: =$E$6
    ptrZ Refers to: =$F$6

    rgnX Refers to: =INDEX(tbl, 0, COLUMNS(ptrTL:ptrX) )
    rgnY Refers to: =INDEX(tbl, 0, COLUMNS(ptrTL:ptrY) )
    rgnX Refers to: =INDEX(tbl, 0, COLUMNS(ptrTL:ptrZ) )

    This seems a little awkward, but it defines the columns in terms of the offsets from the start of the table to their named header cells.
    The approach allows rows to be inserted above, and columns to be inserted to the left or the interior of the table, with ranges adjusting automatically.
    Suppose you want to apply a function (say, SUM) to a particular column. Use this formula in the same column, anywhere above the data:

    =SUM(INDEX(tbl, 0, COLUMNS(ptrTL:Me))
    Last edited by shg; 02-11-2009 at 02:02 PM.
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Best way to name Dynamic ranges

    Thanks to both of you. I'll need to print these up and play with them. I appreciate it.
    Is OFFSET one of those Volitile formulas like INDIRECT which recalculates everytime there's a change in the worksheet?
    Last edited by ChemistB; 02-11-2009 at 05:08 PM.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Best way to name Dynamic ranges

    OFFSET and DIRECT are both volatile, INDEX is not.

+ 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