|
| |
Lotto
This workbook demonstrates the following features:
- Reading numbers from sheet cells into VBA variables,
- Selecting a unique set of random numbers,
- Sorting the selection into ascending order.

All VBA code is commented. Click
here to download
Have fun, and remember me if you win!
If you don't want to download it, here's the code:
Option Explicit
Const cByg = "Byg Software's Lotto"
Dim giSelect% ''Number of selections
Dim giTop% ''Largest value
'' ***************************************************************************
'' Purpose : Control loop for lottery selection
'' Written : July 96 by Andy Wiggins - Byg Software Ltd
''
Sub mLottoLoop()
On Error GoTo sOops
giSelect = Range("Select") ''Get the number of selections from the "Select" range
giTop = Range("Highest") ''Get the largest number available from the "Highest" range
''Test for silly numbers
If giSelect > giTop Then
MsgBox "No" & Chr(10) & "The 'Select' number must be lower than 'Highest'", vbExclamation, cByg
Exit Sub
ElseIf giSelect < 1 Then
MsgBox "No" & Chr(10) & "You cannot use negative numbers", vbExclamation, cByg
Exit Sub
End If
''Keep looping while the user presses "Yes"
'' \_____/ This function is called as the "Message".
Do While vbYes = MsgBox(fLotto, vbQuestion + vbYesNo, cByg)
Loop
Exit Sub
sOops:
MsgBox Error(Err), vbExclamation, cByg
End Sub
|
'' ***************************************************************************
'' Purpose : Select and sort lottery numbers
'' Written : 10/07/96 by Andy Wiggins - Byg Software Ltd
''
Function fLotto()
Dim viNum%
Dim viCounter%
Dim viCounter2%
Dim viCounter3%
Dim vtOutput$
Dim vbUnique As Boolean
Dim viOc1%
Dim viOc2%
Dim viTempStore%
ReDim arResults(giSelect) As Integer ''Define the array to hold the results
Randomize ''Ensure we get random numbers
viCounter = 0 ''Set some defaults
viCounter3 = 1
''- - - - - - - - - - - - - - - - - - - - - - - - - - Generate the results set
Do
viCounter = viCounter + 1 ''Increment the counter
Do
vbUnique = False ''Set as a default
viNum = Int(Rnd * giTop) + 1 ''Generate a number
For viCounter2 = 1 To giSelect ''Loop around the results array to see ..
If viNum = arResults(viCounter2) Then ''.. if the latest number has already been selected
vbUnique = True ''If it has, set this variable to TRUE ..
Exit For ''.. and leave the FOR loop
End If
Next
If vbUnique = False Then
arResults(viCounter) = viNum ''Insert in the array if it hasn't alreeady been selected
End If
Loop Until vbUnique = False
Loop Until viCounter >= giSelect
''- - - - - - - - - - - - - - - - - - - - - - - - - - Sort into order
For viOc1 = 1 To giSelect ''Set up two loops
For viOc2 = 1 To giSelect - 1
''Test the latest position (viOc1) against everything to its right
If arResults(viOc1) < arResults(viOc2) Then ''If the value is HIGHER, then we need to move it
''Swap the values
viTempStore = arResults(viOc2) ''Store the value
arResults(viOc2) = arResults(viOc1) ''Move the test value
arResults(viOc1) = viTempStore ''Put the stored value into the test value's position
End If
Next
Next
''- - - - - - - - - - - - - - - - - - - - - - - - - - Arrange data for output
vtOutput = "" ''Set up an empty string
For viCounter = 1 To giSelect
vtOutput = vtOutput & arResults(viCounter) ''Catenate the values
If giSelect <> viCounter Then vtOutput = vtOutput & " : "
Next
fLotto = vtOutput ''Output the results
End Function
|
Published: 20-May-2005
Last edited:
05-Jun-2005 19:28 |