Test if value is between two numbers

  • Thread starter Thread starter smplogc
  • Start date Start date
S

smplogc

What's the code to test if a value is between two numbers? I can easil
find the answer for other languages, but not VBA! TIA
 
smplogc said:
What's the code to test if a value is between two numbers? I can easily
find the answer for other languages, but not VBA! TIA.


Sub testvalue()
If Selection.Value >= 5 Then
If Selection.Value <= 10 Then
Selection.Offset(1, 0).Value = 25 'this is where you can put your code
End If
End If
End Sub


there are dozens of of sites that explain VBA,
here is a great one to start printing off and start reading

http://www.mindspring.com/~tflynn/excelvba2.html#Send_Mail

http://www.excel-vba.com/index.htm
 
This expression

Sgn(No1-YourNum) + Sgn(No2-YourNum)

enumerates all 5 possibilities:

Your number less than the minimum of No1, No2
Your number is equal to the minimum of No1, No2
Your numer is between No1 & No2
Your number is equal to the maximum of No1, No2
Your number greater than the maximum of No1, No2
 
The example test whether a number is between 50 and 100. It first assigns the
value to be tested to a variable "a". In the example, the value of the
selected cell is being tested.

Sub test()
Dim a
a = Selection.Value
If a > 50 And a < 100 Then
MsgBox "Hello"
End If
End Sub


Regards,
Edwin Tam
(e-mail address removed)
http://www.vonixx.com
 
Just to add one more

Dim nMax as Long, nMin as Long, MyNum as Long
nMax = 51
nMin = 39
MyNum = int(rnd()*100+1)
if MyNum > nMin and MyNum < nMax then


end if
 
Back
Top