Consulting

Results 1 to 7 of 7

Thread: Easy! How to make a button that generates a random number?

  1. #1

    Easy! How to make a button that generates a random number?

    This is probably an easy thing to do, but I've never been good at coding, so here I am :-)

    So far I've drawn a box (which I'm calling a button) on my spreadsheet, to which I assigned a macro that I called Activate.

    What I want to happen is, every time I click the Activate button, a random integer will appear in a cell that I called Magic.

    I'm not sure if I need to enter "=Rand()" in the Magic cell and then somehow write a macro that would refresh the number every time I click the Activate button, or if I need to write the Activate macro so that it generates and outputs the number on its own. I would prefer the former, because that will be easier for me to work with. If I can just get the button to refresh the random number output in the Magic cell, I can handle the rest.

    So far I have pretty much nothing:

    Sub Activate()
    Magic.?
    End Sub

    The purpose of this, if you're interested, is that I want to make a numbered list of all the things we like to eat in our family so that when I click a button, a random food item will be selected and we can decide what to eat for dinner! Eliminate the arguing, right?

    Anyways, any help is greatly appreciated! Thanks!

  2. #2
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    Try this attachment (add your own food of course).

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by kickthecow
    The purpose of this, if you're interested, is that I want to make a numbered list of all the things we like to eat in our family so that when I click a button, a random food item will be selected and we can decide what to eat for dinner! Eliminate the arguing, right?
    Okay, now that is damn funny!

    Just in case you are still curious about generating a number from 1 to 100 via code, the following will work for you:

    [vba]Sub InsertRandom()
    Dim lMax As Long
    Dim lMin As Long

    'Set the random number boundaries
    lMin = 1
    lMax = 100

    'Generate the random number
    Randomize
    Range("A1").Value = WorksheetFunction.RandBetween(lMin, lMax)

    End Sub[/vba]

    I'd probably just use the application that david000 built for you.

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    Okay, now that is damn funny!
    We're like that in Illinois

    Some more refinements to the food thing.

    Accepts Variable length columns and some conditional formats.

  5. #5
    david000- Thanks so much for the attachment! It works great and all I have to do is the fun part -- adding the food! You're the best!
    Thanks again!

    Ken Puls - Thanks for the coding tip, also. Coding is not at all like riding a bike. If you don't do it for a while, you forget.

    Thanks again, all, for your help!
    ~Kathryn

  6. #6
    P.S. - The variable length columns is a super add-on!

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by kickthecow
    Ken Puls - Thanks for the coding tip, also. Coding is not at all like riding a bike. If you don't do it for a while, you forget.
    Totally agree! It's one of those "use it or lose it" type skills.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •