+ Reply to Thread
Results 1 to 8 of 8

Random text

Hybrid View

  1. #1
    Registered User
    Join Date
    10-11-2019
    Location
    Reedley, California
    MS-Off Ver
    2013
    Posts
    10

    Random text

    I am working on a spreadsheet to keep track of football stats, as the game is going on. The info on each play is entered in a row, and each column represents different information, like who carried the ball, who threw the ball and I am trying to figure out a way to randomize who made the tackle from a list of the eleven defensive players on the field. I have tried just about everything I can think of to no avail. I need a button to push that will enter a random defender described as :DL1, DL2, DL3, LB!, LB2, etc. Any ideas would be most appreciated.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: Random text

    This will randomly select 1 out of 11 items.

    msgbox application.WorksheetFunction.Choose(application.WorksheetFunction.RandBetween(1,11),"P1","P2","P3","P4","P5","P6","P7","P8","P9","P10","P11")
    replace the Msgbox with the range reference of where the result should be stored.
    Also replace the Player1 to Player11 with the correct position name.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    10-11-2019
    Location
    Reedley, California
    MS-Off Ver
    2013
    Posts
    10

    Re: Random text

    HI Andy. Thanks for your reply. I am really confused. I m fairly new to the whole excel world. Could you send me what the exact formula would look like if I wanted the result to be stored in cell Q17? I assume I would just need to copy and paste the formula into the rest of the cells in column Q to get it to work for each play as it occurs? Sorry about the confusion. I really appreciate the help.

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,420

    Re: Random text

    Quote Originally Posted by Andy Pope View Post
    msgbox application.WorksheetFunction.Choose(application.WorksheetFunction.RandBetween(1,11),"P1","P2","P3","P4","P5","P6","P7","P8","P9","P10","P11")
    VB has its own Choose function which works the same as the Excel worksheet CHOOSE function (for the most part), so you can remove what I highlighted in red and this code line will work the same

    I would note, however, since no variables are involved, you could just do this...
    MsgBox [Choose(RandBetween(1,11),"P1","P2","P3","P4","P5","P6","P7","P8","P9","P10","P11")]
    Or, use the INDEX function (which I think might be a more efficient function) instead of the CHOOSE function like this...
    MsgBox [INDEX({"P1","P2","P3","P4","P5","P6","P7","P8","P9","P10","P11"},RandBetween(1,11))]
    Last edited by Rick Rothstein; 10-14-2019 at 05:33 AM.

  5. #5
    Registered User
    Join Date
    10-11-2019
    Location
    Reedley, California
    MS-Off Ver
    2013
    Posts
    10

    Re: Random text

    Thank you Andy and Rick - it works great!

  6. #6
    Registered User
    Join Date
    10-11-2019
    Location
    Reedley, California
    MS-Off Ver
    2013
    Posts
    10

    Re: Random text

    Hi Rick - is there a function that will choose the items in parentheses in order instead of randomly?

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: Random text

    You have posted in the VBA forum and mention pushing a button. So the solution is a piece of VBA syntax to update a cell.
    You would need to place the code in the routine assigned to the button and change msgbox to be the cell reference.

    Sub MyButton_Click()
       Range("Q17") = application.WorksheetFunction.Choose(application.WorksheetFunction.RandBetween(1,11),"P1","P2","P3","P4","P5","P6","P7","P8","P9","P10","P11")
    End Sub
    You could remove the Application.Worksheet references and place the 2 formula in a cell, but the value will change every time the RandBetween formula is triggered.

    A1: =Choose(RandBetween(1,11),"P1","P2","P3","P4","P5","P6","P7","P8","P9","P10","P11")

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: Random text

    You would replace the RANDBETWEEN with the index value of the item you want. This applies to the CHOOSE and INDEX functions

+ 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. Vlookup when text is random
    By mir00005 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-28-2014, 04:27 PM
  2. Random Numbers Into Text
    By 500bloc in forum Excel General
    Replies: 8
    Last Post: 01-20-2012, 01:34 AM
  3. How to generate random text
    By on_way_to_fame in forum Excel General
    Replies: 15
    Last Post: 09-27-2009, 04:16 PM
  4. Excel 2007 : Filter Random Data Text into a Text Table
    By Miles Tails in forum Excel General
    Replies: 0
    Last Post: 07-26-2009, 03:42 PM
  5. Random Text Selection
    By jonathanrams in forum Excel General
    Replies: 2
    Last Post: 03-03-2008, 06:30 PM
  6. Getting the text from a random cell / row
    By Eirik in forum Excel General
    Replies: 4
    Last Post: 10-07-2007, 07:31 AM
  7. Random text not quite random
    By Stevesmith in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-30-2007, 03:03 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