+ Reply to Thread
Results 1 to 6 of 6

Highlight random cells within a table

  1. #1
    Registered User
    Join Date
    02-06-2015
    Location
    Bollington
    MS-Off Ver
    2010
    Posts
    10

    Highlight random cells within a table

    Hi There,

    I have data tables manually collated in Excel. I want to be able to highlight cells at random (e.g. 5% of all cells in each table) to perform a spot-check against the raw data in my statistical software for the purposes of QC. I'm aware of functions such as RAND() to produce random values, but unsure how that could be utilised for my need. Therefore, thinking a VBA approach is required.

    Any thoughts here would be appreciated.

    Thanks,

    Rob.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,628

    Re: Highlight random cells within a table

    Try such code.
    Please Login or Register  to view this content.
    I probably don't have to say this, but … just for those not into statistics: Selecting 5% of random cells don't mean they will be evenly distributed in the range - there could be sometimes clusters of few cells (most often - two of course) next to each other and sometimes quite large regions without even single cell highlighted.

    If one wants to not allow such clusters, an easy way would be (but then column A and row 1 cannot be included in the selected range) using so called hard core approach - as in the below line
    Please Login or Register  to view this content.
    instead of
    Please Login or Register  to view this content.
    Of course, in the code above you can use other means of defining the range where highlighting is to be done, like always Range("A1:X100") etc., not just selecting range before running the procedure.
    Attached Files Attached Files
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    02-06-2015
    Location
    Bollington
    MS-Off Ver
    2010
    Posts
    10

    Re: Highlight random cells within a table

    This is perfect, thank you Kaper - much appreciated.

    Thanks,

    Rob.

  4. #4
    Registered User
    Join Date
    02-06-2015
    Location
    Bollington
    MS-Off Ver
    2010
    Posts
    10

    Re: Highlight random cells within a table

    Hi Kaper,

    I'm trying to take your code segment a little further and restrict to the non-empty cells only, i.e. not to highlight cells for QC with no data in. If a randomly selected cell is empty, I want the code to skip to the next iteration in the Do loop until a non-empty cell is found. However, running into problems with line 22 (the IF statement after the second ELSE statement), whereby the program breaks should an empty cell be randomly selected. I think this is because no out_range is set if the first cell randomly selected is blank, but unsure how to get the program to move onto the next loop if an error occurs.

    Please Login or Register  to view this content.

    Thanks,

    Rob.
    Last edited by Rob Wood; 04-16-2020 at 03:40 AM.

  5. #5
    Registered User
    Join Date
    02-06-2015
    Location
    Bollington
    MS-Off Ver
    2010
    Posts
    10

    Re: Highlight random cells within a table

    Solved with a bit of perseverance and Google... Needed to use two Do Loops.

    Please Login or Register  to view this content.

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,628

    Re: Highlight random cells within a table

    Hi Rob,
    Glad you did it and thanks for marking thread solved and for reputation point. Below is a small ammendment to original code which should also work
    Please Login or Register  to view this content.

+ 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. Highlight cells if same value is not present in another table
    By acct-guy in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-28-2016, 05:33 PM
  2. Highlight cells across table when certain cells are clicked
    By John19 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-23-2016, 09:08 PM
  3. Highlight Visible Cells from filtered column in a table
    By BG1983 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-19-2016, 10:52 PM
  4. [SOLVED] Highlight Cells based on data in different Table
    By rz6657 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-16-2015, 08:20 PM
  5. Replies: 2
    Last Post: 06-19-2014, 10:04 AM
  6. Search Surrounding Cells and Use Logic Table to Highlight cells
    By Hekagigantes in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-14-2014, 08:36 AM
  7. [SOLVED] How Excel 2003 Highlight random cells and running tally appear?
    By PULIDOC in forum Excel General
    Replies: 1
    Last Post: 04-27-2006, 07:35 PM

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