View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Select a random cell < 1

Sub aaatest()
Dim rng As Range, rng1 As Range
Dim num As Long, dim i as long
'For i = 1 To 100
Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
num = Int(Rnd() * rng.Count + 1)
Cells(num, 1).Interior.ColorIndex = 5
Do While True
If IsNumeric(Cells(num, 1)) Then
If Cells(num, 1) < 1 Then
Set rng1 = Cells(num, 1)
Exit Do
End If
Else
Set rng1 = Cells(num, 1)
Exit Do
End If
num = num + 1
If num rng.Count Then _
num = 1
Loop
If Not rng1 Is Nothing Then
' rng1.Offset(0, 1).Value = rng1.Offset(0, 1).Value + 1
msgbox rng1.Address
Else
MsgBox "all filled with 1"
End If
'Next
End Sub

This approach is not uniformly random. Cells located after a cell with a 1
in it will have a higher probability of being picked. Don't know if this is
an issue or not. If it is, it could be modified to continue to draw random
numbers until it hits a cell without a 1.

--
Regards,
Tom Ogilvy




"Jason Morin" wrote in message
...
I'm not a programmer so I'm struggling with this.

I have a dynamic range of cells (say A1:A600, but will
expand beyond A600). These cells get individually
populated over time with a 1. I'd like a routine to
identify all cells in the range *without* a 1, and then
from this collection, choose a random cell (or row #,
since it's all in col. A).

Any sample code would be appreciated.
Thanks.
Jason