View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Auric__ Auric__ is offline
external usenet poster
 
Posts: 538
Default compare x to values in a range

d.e.lindstrom wrote:

X is a random integer
I want to compare X to the value in each cell in the range C965:C989 and
if X matches the value in any one of those cells go back to the routine
where x is generated again.


Your question pretty much answers itself. This is an English-to-VBA
translation:

Dim cel As Range
jump_target:
' X is a random integer
X = Int(Rnd * 10)
For Each cel In Range("C965:C989")
' I want to compare X to the value in each cell in the range
' C965:C989 and if X matches the value in any one of those cells go
' back to the routine where x is generated again.
If X = cel.Value Then GoTo jump_target
Next
Stop

Note that some people will bitch because OH NOES TEH EEEEEVIL GOTO but it's
short, simple, and it works. (You can avoid GOTO via DO/LOOP or similar.)

--
The good news is that events are unfolding just as foretold.
The bad news is that the story ends in our utter ruin!