+ Reply to Thread
Results 1 to 10 of 10

command Button to randomly pick from list of names

  1. #1
    Registered User
    Join Date
    08-17-2014
    Location
    NSW, Australia
    MS-Off Ver
    Excel 2010
    Posts
    27

    command Button to randomly pick from list of names

    Hi. I am trying to create a quiz for myself for studying a foreign language. and I am trying to create a command button, so that when clicked, it will randomly pick from a list of names (located in another sheet) and place the name in a desired cell. Unfortunately I don't understand excel or VBA that well. Can anyone point me in the right direction as to how to do this?

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,896

    Re: command Button to randomly pick from list of names

    Here is one way to do it.

    Put your Names in column A of your spreadsheet
    In cell B1 type =rand() and copy it down so that this formula is in every cell in column B for which there is a name in column A.

    Insert this code in your VBA editor

    Option Explicit

    Please Login or Register  to view this content.
    How to install your new code
    1. Copy the Excel VBA code
    2. Select the workbook in which you want to store the Excel VBA code
    3. Press Alt+F11 to open the Visual Basic Editor
    4. Choose Insert > Module
    5. Edit > Paste the macro into the module that appeared
    6. Close the VBEditor
    7. Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)

    To run the Excel VBA code:
    1. Press Alt-F8 to open the macro list
    2. Select a macro in the list
    3. Click the Run button
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    08-17-2014
    Location
    NSW, Australia
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: command Button to randomly pick from list of names

    Thanks heaps for your quick reply Alan. However, when I push Alt + F8 to bring up the macro list. There isn't anything in it. Have I gone wrong somewhere?
    Last edited by jamiegfinch; 08-18-2014 at 01:32 AM.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,896

    Re: command Button to randomly pick from list of names

    Did you install the macro according to my instructions. Must be in a module and not in a worksheet.

  5. #5
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: command Button to randomly pick from list of names

    another
    Please Login or Register  to view this content.
    Last edited by martindwilson; 08-18-2014 at 09:35 AM.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  6. #6
    Registered User
    Join Date
    08-17-2014
    Location
    NSW, Australia
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: command Button to randomly pick from list of names

    Sorry, I don't know what I did before. But it's working now. But that wasn't what I had in mind. What I'm trying to achieve is:- If I click on the developer tab, and under the Insert tab I click the command button (ActiveX control). And lets just say that I place the command button in cell D4. What I would like to happen, is that when I click that button. It will pick one of those names in the list at random and place it into cell D2. Does that make sense? The spreadsheet is actually a lot more complex than this, but this is one area where I'm stuck. Is there a way for me to post the spreadsheet?

  7. #7
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: command Button to randomly pick from list of names

    try
    Please Login or Register  to view this content.
    change the sheets a to the names you actually have

  8. #8
    Registered User
    Join Date
    08-17-2014
    Location
    NSW, Australia
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: command Button to randomly pick from list of names

    I'm terribly sorry for my lack of understanding in this manner. But are you saying to change the words "A" to the names of the sheets or "sheet1" and "sheet2" to the names of the sheets I have?
    If for example, I have labelled the 1st sheet "Data A" and the 2nd sheet "Data B". And In the "Data B" sheet I have the list of names in A1 to A10. In the "Data A" sheet is where I will have the command button with the random result to appear in cell D2. How will I word the coding?

  9. #9
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: command Button to randomly pick from list of names

    it would look like this
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    08-17-2014
    Location
    NSW, Australia
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: command Button to randomly pick from list of names

    Mate! Brilliant. Works like a charm. Thanks heaps!

+ 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. VBA to group a list of names randomly
    By Ironman in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-30-2014, 03:54 AM
  2. Replies: 0
    Last Post: 03-25-2014, 04:21 PM
  3. Pick 2 numbers randomly from list and iterate
    By mrtn88 in forum Excel General
    Replies: 1
    Last Post: 07-10-2012, 10:25 AM
  4. I have a column full of names and need to randomly pick 300 of them.
    By JoseK70 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-22-2011, 08:17 PM
  5. Excel 2007 : Randomly pairing up list of names
    By Stefanious in forum Excel General
    Replies: 1
    Last Post: 09-08-2011, 03:41 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