+ Reply to Thread
Results 1 to 20 of 20

VBA to randomly select 10 employees from a list of employees

  1. #1
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    VBA to randomly select 10 employees from a list of employees

    Hello everyone,

    I would greatly appreciate if someone can help to create a VBA code to randomly select 10 employees from a list of employees. Every time the VBA code is executed, it should select no duplicates or repeats and then copy and paste into two worksheets with the appropriate random selection. The random selection should be based on the following criteria:
    1. Employee status should be active (not on leave or suspended)
    2. Scope should be included
    Finally the generated worksheets (RandomList1 and RandomList2) should be broken down by shift and language. For example, RandomList1 worksheet should include 10 active employees from A shift with included scope and only 1 language. RandomList2 worksheet should include 10 active employees from B shift with included scope and only 1 language.
    I have attached a sample file to demonstrate a final result.
    Again, thank you for your valuable time and dedication to this wonderful forum.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: VBA to randomly select 10 employees from a list of employees

    Hi Kimston,

    Here's a proposal in the attached file. The code is in module1. There might be simpler way to do it...
    I've broken this down by steps for your understanding.
    Let me know if you have any questions.
    Attached Files Attached Files
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Re: VBA to randomly select 10 employees from a list of employees

    Thank you so much for your quick response. Everything seems to work perfectly with one thing that RandomLlist1 worksheet should only have A shift employees and RandomLlist2 worksheet should only have B shift employees. Also, does this code ensures that every employee in the list, regardless of the number of employees, will get a chance to be selected before re-selecting a previous selected employee again? I would also like to have a 3rd worksheet (selected employees) to keep track of all the names of selected employees and the date that were selected from RandomLlist1 and RandomList2. See the attached example.
    Again, thank you so much for your help!
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: VBA to randomly select 10 employees from a list of employees

    Hi,
    Forget my previous code. A few errors I didn't see... ((I guess I need more coffee)
    here's the new one...


    The code does not ensure that employees will not be selected twice. I will have to modify the code for this but will not have the time before later tonight or tomorrow...
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Thumbs up Re: VBA to randomly select 10 employees from a list of employees

    No worries. I'm patient and thankful for your help and everyone else. You deserve a delicious coffee.

  6. #6
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: VBA to randomly select 10 employees from a list of employees

    Hello,

    Please have a try with this file.
    Attached Files Attached Files

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: VBA to randomly select 10 employees from a list of employees

    Shorter code + cell format.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by jindon; 08-12-2018 at 02:01 AM. Reason: Missed random max of 10 for each shift.

  8. #8
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Re: VBA to randomly select 10 employees from a list of employees

    Thank you so much for your outstanding help. Both of the provided codes and files worked perfectly. I was wondering if there is a way to copy and paste the selected employees from RandomList1 and RandomList2 into selected employees worksheet to keep track of all the names of selected employees and the date that were selected?

  9. #9
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Re: VBA to randomly select 10 employees from a list of employees

    Hello Jindon,
    Thank you so much for excellent help. What part of your code do I need to update if I want to increase the number of employees in the future? Let's say that I need to include 15 instead of 10 employees?

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: VBA to randomly select 10 employees from a list of employees

    Try replace "test" sub with
    Please Login or Register  to view this content.
    Last edited by jindon; 08-14-2018 at 01:03 AM.

  11. #11
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Re: VBA to randomly select 10 employees from a list of employees

    Thank you so much GC Excel and Jindon for your help with this case. Your help has helped me tremendously. Jindon, you last code complemented to accomplish the final results.

  12. #12
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Re: VBA to randomly select 10 employees from a list of employees

    Hi Jindon,
    I tried to run your code and I am getting a Run-time error '13' Type mismatch with yellow highlighted on
    Please Login or Register  to view this content.
    right underneath
    Please Login or Register  to view this content.
    Can you point out what causing it?
    Thank you again for helping

  13. #13
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Re: VBA to randomly select 10 employees from a list of employees

    I replaced "test sub as you mentioned above and updated
    Please Login or Register  to view this content.
    10 to 15. I get the the Run-time error '13' Type mismatch.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: VBA to randomly select 10 employees from a list of employees

    I'm out at the moment, so I'll get back to you later.

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: VBA to randomly select 10 employees from a list of employees

    Most probably it happens when no data for "A" and/or "B".
    Try replace "test" with the code below
    Please Login or Register  to view this content.
    Last edited by jindon; 08-14-2018 at 01:04 AM.

  16. #16
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Re: VBA to randomly select 10 employees from a list of employees

    Thank you so much Jindon. You are a true life saver. I noticed that when I changed the A and B for the shiftcode to 1 and 2 or I1 OR I2, the code doesn't behave as it should. If I change A and B to 1 and 2, the RandomLlist1 doesn't get any data populated. It stays blank. However RandomLlist2 gets random selected data with 2 and 1 as for the shiftcode. What part of your code analyzes and collects the shiftcode? What do I need to do if I want to use I1 and I2 instead A and B?
    Again,
    Thank you so much for your contribution to this Excelforum!

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: VBA to randomly select 10 employees from a list of employees

    The code is written based on only 2 shifts. One is "A" and other, so
    Please Login or Register  to view this content.
    If you change "A" with the one you want to change, e.g. 1, it will create a report for 1 and the other.

  18. #18
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Re: VBA to randomly select 10 employees from a list of employees

    I tried 1 and 2 for A and B or I1 and I2 for A and B, but data just get populated in RandomLlist2 and in Selected employees; no data in RandomList1. See attachment for latest sample.
    Attached Files Attached Files

  19. #19
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: VBA to randomly select 10 employees from a list of employees

    Ahh, one more to change.
    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Re: VBA to randomly select 10 employees from a list of employees

    Jindon,
    Your final revision definitively does the final results I was looking for. Thank you very much for all your hard work and dedication to Excelforum!

+ 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. Replies: 11
    Last Post: 11-03-2017, 05:11 AM
  2. Replies: 10
    Last Post: 07-27-2017, 12:44 PM
  3. Forecast How Many Employees To Hire Based on Active and Termed Employees
    By Kanook22 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-12-2014, 01:19 PM
  4. Schduling Employees - Fairly - Randomly
    By nickmessick1 in forum Excel General
    Replies: 4
    Last Post: 06-18-2013, 02:47 PM
  5. Replies: 2
    Last Post: 03-11-2013, 09:59 PM
  6. Replies: 4
    Last Post: 11-28-2010, 11:00 PM
  7. Select a name from a list of employees using Excel
    By Tornado in forum Excel General
    Replies: 1
    Last Post: 01-07-2005, 04:06 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