ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   compare x to values in a range (https://www.excelbanter.com/excel-programming/450817-compare-x-values-range.html)

[email protected]

compare x to values in a range
 
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.

Auric__

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!

[email protected]

compare x to values in a range
 
On Saturday, April 25, 2015 at 7:30:53 AM UTC+10, 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.


thx it works


All times are GMT +1. The time now is 08:52 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com