+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Forum Contributor
    Join Date
    07-02-2008
    Location
    Utah
    Posts
    171

    Return a Random Column Value

    Hi,

    I wrote the following formula in cell F2: IFERROR(INDEX(DW.Data!1:1,0,MATCH(M1,DW.Data!1:1,0)),0).

    For the row option of the index formula, I need to add some logic that will basically choose a random row to return among ONLY rows that have values. The column will have maybe 10,000 rows with 9,652 rows being blank (no value) and 348 rows have a value. The values within the row will be noncontiguous.

    I need the above formula to offset and choose any random cell containing a value and return it.

    I've attached an example. THANKS FOR ALL YOUR HELP!!
    Attached Files Attached Files
    Last edited by John Bates; 03-16-2010 at 06:04 PM. Reason: SOLVED

  2. #2
    Forum Guru contaminated's Avatar
    Join Date
    05-07-2009
    Location
    Baku, Azerbaijan
    MS-Off Ver
    Excel 2010
    Posts
    1,335

    Re: Return a Column Value

    Maybe this one

    =INDEX(B2:B10000,SMALL(IF(B2:B10000<>"",ROW(INDIRECT("1:"&ROWS(B2:B10000)))),RANDBETWEEN(1,SUM((B2:B 10000<>"")*1))))

    Confirm with ctrl + Shift + enter
    Люди, питающие благие намерения, как раз и становятся чудовищами.

    Regards, «Born in USSR»
    Vusal M Dadashev

    Baku, Azerbaijan

  3. #3
    Forum Moderator NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003, 2007, 2010
    Posts
    31,148

    Re: Return a Column Value

    This seems to work:

    Code:
    =INDEX(B2:B10000,SMALL(IF(B2:B10000<>"",ROW(B2:B10000)-ROW(B2)+1),RANDBETWEEN(1,COUNTIF(B2:B10000,"*?"))))
    confirmed with CTRL+SHIFT+ENTER

    You have some cells in B range with 0, remove those.. or you may get 0 returned sometimes.
    Last edited by NBVC; 03-16-2010 at 05:04 PM. Reason: Added note concerning 0's found in B2:B10000
    Microsoft MVP - Excel

    Where there is a will there are many ways. Pick One!


    Please read the Forum Rules

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

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

    Preferred Charities: Lupus Canada and Sick Kids Foundation.
    Feel Free to Donate if you want to, for the assistance you received today.

  4. #4
    Forum Guru martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    10,477

    Re: Return a Random Column Value

    i'd opt for a helper column in say col b
    if(a1>0,rand(),"") dragged down then match for use in the index row criteria as MATCH(MIN(B1:B10000),B1:B10000,0) [or MATCH(max(B1:B10000),B1:B10000,0)]
    Mojito connoisseur and a dabbler in Cisco
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  5. #5
    Forum Moderator NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003, 2007, 2010
    Posts
    31,148

    Re: Return a Random Column Value

    The array formula takes mere milliseconds to refresh upon hitting F9.

    If you have many other formulas evaluating in the sheet or if you have lots more than 10000 rows, then perhaps helper column would be more ideal....
    Microsoft MVP - Excel

    Where there is a will there are many ways. Pick One!


    Please read the Forum Rules

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

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

    Preferred Charities: Lupus Canada and Sick Kids Foundation.
    Feel Free to Donate if you want to, for the assistance you received today.

  6. #6
    Forum Contributor
    Join Date
    07-02-2008
    Location
    Utah
    Posts
    171

    Thumbs up Re: Return a Random Column Value

    Perfect, thank you for both of your help. I ended up using contaminated's array formula for this particular problem though both worked equally well.

    Thanks again!!!

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.2.0