+ Reply to Thread
Results 1 to 15 of 15

Macro to get random cell in a range (Ignoring empty cells)

  1. #1
    Registered User
    Join Date
    10-19-2012
    Location
    Egypt
    MS-Off Ver
    Excel 2003
    Posts
    40

    Macro to get random cell in a range (Ignoring empty cells)

    Hi,
    I've been looking for an answer to this problem since 3 weeks but no solution was found.
    in reference to my previous post, I desperately need to pick a random cell within columns A to C (the whole columns) and extract its content to result cell (perhaps E1). However, I need this macro to recognize and exclude empty cells in selected columns.

    This should happen automatically every time the sheet get refreshed (no buttons used).

    I'd truly appreciate any help with this case
    Thanks,

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to get random cell in a range (Ignoring empty cells)

    Here you go.

    You need an array formula to work out the maximum populated row, something similar to

    =MAX(MAX((A1:A25<>"")*ROW(A1:A25)),MAX((B1:B25<>"")*ROW(B1:B25)),MAX((C1:C25<>"")*ROW(C1:C25)))

    you need to ctrl + shift + enter this formula, all this does is limit the amount of blanks to skip, you can not use by hardcoding intMaxRow to whatever row number you want

    Which i have in F1

    the code would be as

    Please Login or Register  to view this content.

    Hope this helps
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Macro to get random cell in a range (Ignoring empty cells)

    I would use a dynamic range and call it ACRange. In this example, in the Name Manager, add the commented formula in the RefersTo. Then run that first Sub to see if it is finding the correct range. It would take alot of iterations to truly be random if you really wanted to check all of A:C.

    Of course enter the UDF as commented for that function or test with the test sub.

    The UDF will only recalculate if you press F2 and Enter key to recalculate that formula in the UDF or change any value in the dynamic range.

    I used the letter "L" in lowercase which may be confusing but if you copy and paste the code in a Module, it should be clear.

    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to get random cell in a range (Ignoring empty cells)

    That way doesnt give you the last row, it only counts the cells that are populated so if you have 1,2,3 in a1:a3, and then 4 in b100, your range will only be 4 rows deep

  5. #5
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Macro to get random cell in a range (Ignoring empty cells)

    Right, dynamic range methods can be tricky if you don't know the data. Obviously, I don't know the data.

    Using my function as a UDF for the named range of ACRange would be:
    =RandNonEmptyZeroValueInRange(ACRange)

    I find this method easier when setting dynamic named ranges. Put this code in your Sheet object. The function code earlier goes in a Module.
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    10-19-2012
    Location
    Egypt
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Macro to get random cell in a range (Ignoring empty cells)

    Thanks a lot for your response Nathansav
    I need every time I refresh sheet (using f9 button) to re-operate the process and get different value, instead of clicking a button.
    Would that be possible?

    Thank you

    Quote Originally Posted by nathansav View Post
    Here you go.

    You need an array formula to work out the maximum populated row, something similar to

    =MAX(MAX((A1:A25<>"")*ROW(A1:A25)),MAX((B1:B25<>"")*ROW(B1:B25)),MAX((C1:C25<>"")*ROW(C1:C25)))

    you need to ctrl + shift + enter this formula, all this does is limit the amount of blanks to skip, you can not use by hardcoding intMaxRow to whatever row number you want

    Which i have in F1

    the code would be as

    Please Login or Register  to view this content.

    Hope this helps

  7. #7
    Registered User
    Join Date
    10-19-2012
    Location
    Egypt
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Macro to get random cell in a range (Ignoring empty cells)

    I appreciate your solution Kenneth and I'd love to use it as well.
    Data are variable, could be 1 row and could be 1000 rows with blanks in between in different columns
    However, as I asked Nathansav, would you please give me a lead on how to get it the macro to work automatically, selecting random value every time sheet get refreshed using F9 (keyboard button)?

    Thank you so much

    Quote Originally Posted by Kenneth Hobson View Post
    Right, dynamic range methods can be tricky if you don't know the data. Obviously, I don't know the data.

    Using my function as a UDF for the named range of ACRange would be:
    =RandNonEmptyZeroValueInRange(ACRange)

    I find this method easier when setting dynamic named ranges. Put this code in your Sheet object. The function code earlier goes in a Module.
    Please Login or Register  to view this content.

  8. #8
    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: Macro to get random cell in a range (Ignoring empty cells)

    Have you looked back at your other thread?
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Macro to get random cell in a range (Ignoring empty cells)

    Either remove
    Please Login or Register  to view this content.
    in my function or make it
    Please Login or Register  to view this content.
    for it to update for any calculation event.

    I am not sure what you mean about the blank cell/row issue. The worksheet code gives you a dynamic range that you can use in my function in a cell formula.
    =RandNonEmptyZeroValueInRange(ACRange)
    Last edited by Kenneth Hobson; 08-21-2013 at 02:37 PM.

  10. #10
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Macro to get random cell in a range (Ignoring empty cells)

    Here is a working example.
    E1 will refresh each time the worksheet is calculated.
    Attached Files Attached Files
    Gary's Student

  11. #11
    Registered User
    Join Date
    10-19-2012
    Location
    Egypt
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Macro to get random cell in a range (Ignoring empty cells)

    Thank you Jakobshaven
    This is exactly how I want it to be processed, except when cell values in range are text the selected result is "#value". I need this to happen when the selected cell contains either text or number.


    Quote Originally Posted by Jakobshavn View Post
    Here is a working example.
    E1 will refresh each time the worksheet is calculated.

  12. #12
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Macro to get random cell in a range (Ignoring empty cells)

    Here is a version that will handle both text and numbers.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    10-19-2012
    Location
    Egypt
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Macro to get random cell in a range (Ignoring empty cells)

    This solves my problem completely!
    Thanks a lot Jakobshaven
    I appreciate the effort

    Quote Originally Posted by Jakobshavn View Post
    Here is a version that will handle both text and numbers.

  14. #14
    Registered User
    Join Date
    06-23-2017
    Location
    USA
    MS-Off Ver
    2012
    Posts
    1

    Re: Macro to get random cell in a range (Ignoring empty cells)

    Quote Originally Posted by Jakobshavn View Post
    Here is a version that will handle both text and numbers.
    This might be too old of a thread but it is exactly what i needed, but I cant figure out how you choose which cell the selected value goes to?

  15. #15
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Macro to get random cell in a range (Ignoring empty cells)

    Hi seabbott:

    Because the code is a function (UDF), the value goes into the cell containing the formula.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Pick Random Value from a Range (not including empty Cells)
    By ghoneim in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-22-2013, 04:46 AM
  2. MACRO to empty a range of cells based on whether specified cell is empty
    By TBJV in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-01-2013, 10:35 PM
  3. [SOLVED] Macro to find empty cells in a range and repalce with the value of a populated cell
    By Markvx in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-23-2012, 06:44 AM
  4. Ignoring Empty Cells
    By cheshajim in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-02-2007, 12:27 PM
  5. [SOLVED] Ignoring empty cells
    By beeo in forum Excel General
    Replies: 3
    Last Post: 09-08-2005, 05:05 AM

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