macros randbetween formulas

R

Roadrunner_Ex

i would like to make a "button" on the spreed sheet it self were every time i
press it, it runs the Randbetween formual and place it in a cell, one is this
possable and can someone show me what i got to do, compleatly have not idear
how to wright mircos thanks
 
A

AltaEgo

It is possible but you have to be more specific about your needs. At
present, all I can work out is that you want a button to enter a
Randbetwee(0 formula in an unspecified blank cell. For example, how does the
function get its values; which blank cell?

When you give further detail, think of yourself in the position of somebody
who knows absolutely nothing about your project and needs.

This may give you a start:

Sub test()

' Get input from user:
vFirst = InputBox("enter smaller value")
vLast = InputBox("enter larger value")

'write a Randbetween formula in the selected cell:
Selection.Formula = "=Randbetween(" & vFirst & "," & vLast & ")"

End Sub
 
R

Roadrunner_Ex

thats correct thats what i would like to happon, the values i want it to
grearate is between 1 and 20 and i don't know which cell yet because i did
not know it was possable, but lets just say cell A1 for know. its all for a
paper to game that i play so i need excell to genrate an number between one
and 20 in to an cell then that number can be use in a simple addition formula
but i won't the random number to change ever time i click the "button" to
give an new number between 1 and 20 and so on, i hope thats a clear idear i
would like to happon, please if this dose not help ill try to again to
explane thanks.
 
A

AltaEgo

As I now understand it, you wish to generate a random number that holds its
value between clicks of a button.

VBA does not have access to Randbetween(). If you need to set a number with
different upper and lower limits:

Sub genRand()
lower = 1 'change
upper = 20 ' change
Randomize
Sheets("Sheet1").Range("A1").Value = Int((upper - lower + 1) * Rnd() +
lower)
'change Sheet1 and A1
End Sub

If you wish to get your upper and lower limits from cells (say, in a sheet
named 'Gamevariables'), instead of hard-coding values you can set upper and
lower this way:
lower = Sheets("Gamevariables").Range("A2").Value
upper = Sheets("Gamevariables").Range("A3").Value


If your lower limit is always 1, the simpler version:
Sub genRand()
Randomize
Sheets("Sheet1").Range("A1").Value = Int(Rnd() * 20) + 1

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top