Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.newusers,microsoft.public.excel.programming
|
|||
|
|||
Random Function in VB
I am trying to use the Random Function in VB to generate a series of Random
numbers equal to a counter that is date based. and insert the random numbers into a cell on the same spreadsheet. I am having some problems getting it to work, any help would be appreciated as I am not a VB programmer. The Code is below: Dim StartDate As Date ' Advisor Start Date Dim GroupCount As Integer ' Counter for determining how many advisors with same start date Dim RandomNumber As Integer ' Random number value generated by Randomize function ' ' Macro recorded 2004-05-14 by Jim Campbell ' This Macro was created to generate random numbers to be used ' in determining the order of choice based upon the same date ' within a group ' ' ' Specify Worksheet and column to work with Sub Column_Select() Worksheets("Lottery").Columns (4) ' Start date of Advisor End Sub ' Check_Start_Date Format (DD-MON-YYYY) will determine number ' to be used for next module that generates Random Numbers. ' GroupCount determines how many random numbers are generated ' based upon Start Date ' ' Sub Check_Start_Date() Check = True: GroupCount = 0 ' Initialize variables. Do ' Outer loop. Do While StartDate = StartDate ' Inner loop. GroupCount = GroupCount + 1 ' Increment Counter. If StartDate < StartDate Then 'If condition is True. Check = False ' Set value of flag to False. Exit Do ' Exit inner loop. End If Loop Loop Until Check = False ' Exit outer loop immediately End Sub ' Use GroupCount value as input for ' module that generates Random Numbers ' ' Sub Random_Number() Randomize ' Initialize random-number generator. RandomNumber = Int((GroupCount * Rnd) + 1) ' Generate random number based on GroupCount End Sub ' ' If cell is is not empty, add value to worksheet ' Add column with Random Number value to Lottery.xls ' ' Sub Add_Column() For Each c In Worksheets("Lottery").Range(RandomNumber).Cells ' Specify cells that will be populated with number If c.Value < Null Then With Worksheets("Lottery").Column(5).Add = RandomNumber ' Add random number to Lottery.xls(column 5) End With End If Next c End Sub |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
random between function | Excel Worksheet Functions | |||
VBA "Rnd" Function: Truly Random? | Excel Discussion (Misc queries) | |||
How can I use the random function? | Excel Worksheet Functions | |||
Random Function | Excel Programming | |||
Random function | Excel Programming |