+ Reply to Thread
Results 1 to 6 of 6

Randomization Macro with multiple criteria

Hybrid View

Coco Dreamboat Randomization Macro with... 02-26-2015, 04:43 PM
xladept Re: Randomization Macro with... 02-26-2015, 05:44 PM
Coco Dreamboat Re: Randomization Macro with... 02-26-2015, 07:20 PM
xladept Re: Randomization Macro with... 02-26-2015, 07:35 PM
Coco Dreamboat Re: Randomization Macro with... 02-27-2015, 11:11 AM
xladept Re: Randomization Macro with... 02-27-2015, 04:04 PM
  1. #1
    Registered User
    Join Date
    03-04-2014
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    4

    Randomization Macro with multiple criteria

    I'm very new to VBA so bear with me, but I'm trying to figure out how you would go about randomly choosing from a list based on multiple criteria in other columns. Essentially I want to "pick a name out of a hat" but only if that name meets the criteria. I only need it to return one item from the list, but I need it to be different every time.

    Example.xlsx

    In the attached file I have Names in Column A, Gender in Column B, and Available (Y/N) in Column C. I would like the macro to find a name from Column A that meets the criteria, for example, randomly return any of the names that are "Male" in Column B and have "Y" in Column C. Also, not sure if it's relevant or not, but names will be added to the list over time, so the range will change as well.

    Hopefully that makes sense. Any and all help is appreciated. Thanks!

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Randomization Macro with multiple criteria

    It's a little rough but:

    Sub Coco(): Dim r As Long, C As String, i As Long, K, G As String, A As String
    r = Range("A" & Rows.Count).End(xlUp).Row
    C = InputBox("Enter your criteria - M or F comma and space then Y or F")
    K = Split(C, ", "): G = K(0): A = K(1)
    Do: i = Int(Rnd() * r + 1)
    Loop Until Left(Cells(i, 2), 1) = G And Cells(i, 3) = A
    MsgBox (Cells(i, 1))
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    03-04-2014
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    4

    Re: Randomization Macro with multiple criteria

    Sorry, this is still a bit over my head! Could you maybe walk me through what edits I would need to make to that to make my example work, or maybe even add it in and upload it?

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Randomization Macro with multiple criteria

    Hi Coco,

    Thanks for the rep!

    It already works with your sample, when you answer the question just type M, F - I can take the space out if you find it awkward?

    Directions for running the routine(s) just supplied

    If you haven't used macros before you'll need to go to:
    File- options - trust center -trust center settings - macro settings ,
    the second option down (disable all macros with notification)

    Then - Copy the code to the clipboard

    Open your Workbook

    Press ALT + F11 to open the Visual Basic Editor.

    Select "Module" from the Insert menu

    Type "Option Explicit" then paste the code under it

    With the cursor between Sub and End Sub press F5 (F8 to Single Step)

    OR

    Press ALT + Q to close the code window.

    Press ALT + F8 then double click on the macro name


    Here:CocoRandPick.xlsm
    Last edited by xladept; 02-26-2015 at 07:43 PM.

  5. #5
    Registered User
    Join Date
    03-04-2014
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    4

    Re: Randomization Macro with multiple criteria

    Ahh got it. Thanks!

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Randomization Macro with multiple criteria

    You're welcome!

    BTW - If you want different I/O we can rewrite the I/O part

+ 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] Multiple criteria sort (criteria parsed from string) macro
    By rynofrowan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-18-2014, 09:59 PM
  2. Macro to do randomization and sorting
    By ajang in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-29-2014, 12:22 AM
  3. [SOLVED] create drop down box to select multiple criteria and link macro to each criteria
    By anand_erin in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 11-03-2012, 06:35 PM
  4. [SOLVED] Data Randomization
    By Hannie1004 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-16-2006, 02:55 PM
  5. strange randomization
    By William Benson in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-10-2005, 05: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