Home |
Search |
Today's Posts |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"pm" skrev i melding ...
Harald Staff wrote: Either generate a collection of N non-duplicate random numbers and loop that collection i guess - generate in cells or in some array in VBA? Here's a basic solution, using integers 1 to 20. Imothere's rarely any reason not to use a hidden sheet's cells for these things, sheets sort and calculate far faster than code does. But this is fast enough for a small amount of items: '********** top of module *********** Type Token L As Long SortNum As Double End Type Sub Lottery() Dim Tokens(1 To 100) As Token Dim Tmp As Token Dim i As Long Dim M As Long, N As Long 'assign values: Randomize For i = 1 To 100 Tokens(i).L = i Tokens(i).SortNum = Rnd() Next 'sort by sortnum: For M = 1 To 99 For N = 1 To 99 If Tokens(N).SortNum Tokens(N + 1).SortNum Then Tmp.L = Tokens(N).L Tmp.SortNum = Tokens(N).SortNum Tokens(N).L = Tokens(N + 1).L Tokens(N).SortNum = Tokens(N + 1).SortNum Tokens(N + 1).L = Tmp.L Tokens(N + 1).SortNum = Tmp.SortNum End If Next Next 'pick top 20: For i = 1 To 20 MsgBox Tokens(i).L, , "Token " & i Next End Sub HTH. Best wishes Harald |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
How can I choose more than one item from list in a form in Excel? | Excel Worksheet Functions | |||
Excel range choose button in VBA form | Excel Programming | |||
loop through form objects | Excel Programming | |||
loop random number | Excel Programming | |||
random number generation: loop | Excel Programming |