Mike
Something like this?
Sub test()
Dim lb As msforms.ListBox
Dim NewNum As Long
Dim i As Long
Dim UsedNums() As Long
Set lb = Sheet1.ListBox1
ReDim UsedNums(0)
For i = 0 To lb.ListCount - 1
If lb.Selected(i) Then
Do
NewNum = GetRand
Loop Until CheckNum(NewNum) And NoDups(NewNum, UsedNums)
UsedNums(UBound(UsedNums)) = NewNum
If i < lb.ListCount - 1 Then
ReDim Preserve UsedNums(UBound(UsedNums) + 1)
End If
End If
Next i
For i = LBound(UsedNums) To UBound(UsedNums)
Debug.Print UsedNums(i)
Next i
End Sub
Function GetRand() As Long
Randomize
GetRand = Int(Rnd * 4) + 1
End Function
Function CheckNum(Num As Long) As Boolean
CheckNum = (Num <= 2 And Num 0)
End Function
Function NoDups(Num As Long, DupNums As Variant) As Boolean
Dim i As Long
NoDups = True
For i = LBound(DupNums) To UBound(DupNums)
If DupNums(i) = Num Then
NoDups = False
Exit For
End If
Next i
End Function
--
Dick Kusleika
MVP - Excel
www.dicks-clicks.com
Post all replies to the newsgroup.
"Michael J. Malinsky" wrote in message
...
I have a MultiSelect ListBox. I'd like to compare a VBA generated random
number with the selections in the ListBox to ensure validity. I am
currently using a loop to determine if the first item in the ListBox is
selected. If it is not selected, we move to the second item, etc. If it
is
selected then we run and If...Then statement to check the random value
against the item selected in the ListBox. If the random value is not
valid,
a new number must be generated by VBA. If the random number is valid,
then
we check to ensure that the number has not been previously chosen (I use
an
array to store the chosen numbers). If the number has not been previously
chosen, then we move on with life. If the number has already been chosen
then VBA must select another random number then we must start the process
over again to ensure that the new selected number is first valid against
the
selected items in the ListBox then checking again for a duplicate value.
I was fine when I had a simple program to simply check for duplicate
values.
VBA selected the number then I used a separate function to check for a
duplicate value. My problem started when I threw in the ListBox selection
section. I can't seem to come up with a fluid method of doing what I
described above. I can probably come up with the programming if someone
can
help with the logic.
TIA
Mike.
--
Michael J. Malinsky