+ Reply to Thread
Results 1 to 14 of 14

Using Rand Function to Select 50 Rows

  1. #1
    Registered User
    Join Date
    07-15-2021
    Location
    North Wales
    MS-Off Ver
    Excel 2007
    Posts
    7

    Using Rand Function to Select 50 Rows

    Hi All,

    Apologies first as I am new to Excel and have a fairly limited understanding. I have been tasked to try and find a way to select 50 random tickets from a weekly report containing over 200+ tickets. So far I have been able to do a work around by copying out the Ticket ID(Column A) assigning it a value and hiding the column then using: =INDEX($A:$A, RANK(N32,$N:$N), 1) In this case I've copied my ID field into a separate sheet to column A, put the hidden values in Column N then run the above in Column B.


    This kind of works but I'm then having to take the random ticket ID's that have been generated and do a manual search for the data in the original sheet to copy and paste into a new sheet with all the details.

    So I guess the short version is - I'm trying to grab 50 random rows from a sheet filled with 100+ and output it to it's own sheet, is there a simple way to do this?

    Below is a test example of the type of data I'm dealing with, any help would be massively appreciated as I'm just running in circles at this point

    test 2.PNG
    Attached Files Attached Files
    Last edited by OldBlue; 07-15-2021 at 05:54 AM. Reason: Uploading test workbook

  2. #2
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,036

    Re: Using Rand Function to Select 50 Rows

    One of the problems with RAND is that it is volatile. Change anything, anywhere and the values change... and then so will your results. An alternative is needed!!
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  3. #3
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,036

    Re: Using Rand Function to Select 50 Rows

    Will there be differences in the number of rows on each weekly sheet... 210 one week, 184 the next, etc, etc??

  4. #4
    Registered User
    Join Date
    07-15-2021
    Location
    North Wales
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using Rand Function to Select 50 Rows

    Thanks for the quick reply Glenn, very appreciated. I did notice that and saw somwhere that I could assign a hidden value to the rand output to stop it changing and it does work, however I need to do this everytime I paste the updated ticketID's in as if they don't match up I get a lot of '0's in my output. Do you know of any alternative methods I could use/research?

  5. #5
    Registered User
    Join Date
    07-15-2021
    Location
    North Wales
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using Rand Function to Select 50 Rows

    Yes unfortunately, it's a changing weekly report based on user interaction - can go anywhere between 150-300.

  6. #6
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,036

    Re: Using Rand Function to Select 50 Rows

    No the variable numbers are PERFECT. It's what I was hoping to hear!!

    I always use this to generate UNIQUE non-volatile pseudo-random numbers:

    =MOD(ROW()/PI(),1)

    It has one disadvantage. If it starts on row 2, it always generates the same random number. So, in your case, to introduce a bit of variability, I modified it to:

    =MOD((ROW()+COUNTA(A:A))/PI(),1)

    So, the numbers are fully randomised. The first random number will vary depending on the number of non-blank rows in your sample. Use that as a helper column (Shhet 1, L2, copied down).

    Then this in sheet 2,A2, copied across and down (in this case 3 rows... in your real data by 50 rows)

    =INDEX(Sheet1!A$2:A$7,MATCH(1,--(Sheet1!$L$2:$L$7=SMALL(Sheet1!$L$2:$L$7,ROWS(B$2:B2))),0))
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    07-15-2021
    Location
    North Wales
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using Rand Function to Select 50 Rows

    Glenn thanks so much! I'll try this out on the true data and get back to you - just out of curiosity, could this potentially select duplicate values?

  8. #8
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,036

    Re: Using Rand Function to Select 50 Rows

    Never. The helper sequence is entirely unique. Tested on 1,048,576 rows!!

  9. #9
    Registered User
    Join Date
    07-15-2021
    Location
    North Wales
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using Rand Function to Select 50 Rows

    It works! Thanks Glen, I'm having one issue where when I post in the true data - currently 154 rows, it flags N/A's to me from row 8 onwards. I've had to move the helper column to V and also expanded this down all the way to the bottom to ensure it's assigning the value but I'm still getting N/A's when I expand past column 7. Am I being stupid and missing something?

  10. #10
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,036

    Re: Using Rand Function to Select 50 Rows

    The formulae are all set to go to row 7. Change the 7s to a sensible number.... 500 or 1000.

    You're welcome.



    It would be very nice if you were to just click the Add Reputation button at the foot of any of the posts of members who helped you reach a solution.

    Finally, if that takes care of your original question, please select "Thread Tools" from the menu link above and mark this thread as SOLVED.

  11. #11
    Registered User
    Join Date
    07-15-2021
    Location
    North Wales
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using Rand Function to Select 50 Rows

    It seems to be when I try to amend: =INDEX(Sheet1!A$2:A$7,MATCH(1,--(Sheet1!$L$2:$L$7=SMALL(Sheet1!$L$2:$L$7,ROWS(B$2:B2))),0))

    When I try and increase past 8, flags either a #REF error or N/A - I've pasted in 154 rows, increased the helper column to 154 rows and I'm not sure where I'm going wrong with it...

  12. #12
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,036

    Re: Using Rand Function to Select 50 Rows

    Please post a file showing the issue.

  13. #13
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Using Rand Function to Select 50 Rows

    Hi,

    Why not add a helper column with a RAND() function to the data then sort the data including the helper column, then simply copy the first 50 rows of data, either manually or a simple macro like say.

    Range("A1:A50").EntireRow.Copy mysheetname.Range("A1")

    If you want to preserve the original data order other than by using the date column add an extra helper column that contains a sequential number the after you've extracted your 50 rows resort back on the original order column
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  14. #14
    Registered User
    Join Date
    07-15-2021
    Location
    North Wales
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using Rand Function to Select 50 Rows

    Never mind I was just being daft, this worked great - I'll close the thread, thanks again

+ 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. HELP with the RAND() Function......AGAIN!!!!!
    By denise1082 via OfficeKB.com in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-31-2006, 07:58 AM
  2. [SOLVED] HELP with the RAND() Function!!!!!!
    By denise1082 in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 07-31-2006, 03:00 AM
  3. is there a way to use rand() to select a cell
    By kckar in forum Excel General
    Replies: 5
    Last Post: 02-22-2006, 11:10 AM
  4. [SOLVED] re: Rand function
    By Harlan Grove in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 09-06-2005, 05:05 PM
  5. re: Rand function
    By Harlan Grove in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2005, 02:05 PM
  6. [SOLVED] re: Rand function
    By Susan Hayes in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 11:05 AM
  7. re: Rand function
    By Harlan Grove in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-06-2005, 04:05 AM
  8. [SOLVED] re: Rand function
    By Susan Hayes in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 10:05 PM

Tags for this Thread

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