PDA

View Full Version : Easy! How to make a button that generates a random number?

kickthecow
06-20-2008, 04:56 PM
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!

david000
06-20-2008, 06:35 PM

Ken Puls
06-20-2008, 08:44 PM
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:

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

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

Cheers,

david000
06-20-2008, 10:39 PM
Okay, now that is damn funny!

We're like that in Illinois:rofl:

Some more refinements to the food thing.

Accepts Variable length columns and some conditional formats.

kickthecow
07-01-2008, 07:16 PM
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

kickthecow
07-01-2008, 07:17 PM
P.S. - The variable length columns is a super add-on!

Ken Puls
07-01-2008, 10:01 PM
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! :yes It's one of those "use it or lose it" type skills.