PC Review


Reply
Thread Tools Rate Thread

Storing a Rand result

 
 
Paul T
Guest
Posts: n/a
 
      20th Jul 2005
Hi

I am trying to simulate a dice roll with: =RAND()*(6-1) which works fine.
I.E. in Cell A1: =RAND()*(6-1)

The problem I have that I want to store the first result in the next cell
I.E. in cell A2 result of A1 but
every time I click on the button to roll the dice A1 changes which is ok -
but A2 does not keep the result of the first roll. This is my problem. Is
there any way round this?

I hope you get my drift.

Thanking you all in anticipation.

Paul




 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      20th Jul 2005
First, I would think your formula would look more like:

=INT(RAND()*(6-1+1)+1)

VBA's help for the RND function shows this to get a random number between two
numbers.

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

And since you're clicking a button to fill that cell, I think I'd have the macro
do the work.

This puts an integer between 1 and 6 in the next available cell in column A. It
puts it as a value--not a formula.

Option Explicit
Sub testme()
Dim myCell As Range

With ActiveSheet
Set myCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
myCell.Value = Int((6 - 1 + 1) * Rnd + 1)
'if you want the formula that will change when you recalculate:
'myCell.Formula = "=INT(RAND()*(6-1+1)+1)"

End Sub

Paul T wrote:
>
> Hi
>
> I am trying to simulate a dice roll with: =RAND()*(6-1) which works fine.
> I.E. in Cell A1: =RAND()*(6-1)
>
> The problem I have that I want to store the first result in the next cell
> I.E. in cell A2 result of A1 but
> every time I click on the button to roll the dice A1 changes which is ok -
> but A2 does not keep the result of the first roll. This is my problem. Is
> there any way round this?
>
> I hope you get my drift.
>
> Thanking you all in anticipation.
>
> Paul


--

Dave Peterson
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RAND() result changes when other cells are used in worksheet. sbickley Microsoft Excel Misc 3 19th Oct 2009 02:34 AM
Storing result of function directly in a variable instead of cell. Mak Microsoft Excel Programming 4 7th Mar 2008 03:33 PM
Storing the result of a calculated field =?Utf-8?B?UGF1bC1X?= Microsoft Access Queries 4 20th Feb 2006 04:26 PM
storing SQL result into a VB variable? =?Utf-8?B?c2FteW1lbGJvdXJuZQ==?= Microsoft Access VBA Modules 5 13th Mar 2005 12:56 PM
Storing the query result using VBA Transliteration Microsoft Access Queries 4 26th Sep 2004 04:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:47 PM.