ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Test if value is between two numbers (https://www.excelbanter.com/excel-programming/360166-test-if-value-between-two-numbers.html)

smplogc[_3_]

Test if value is between two numbers
 

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

--
smplog
-----------------------------------------------------------------------
smplogc's Profile: http://www.excelforum.com/member.php...fo&userid=3388
View this thread: http://www.excelforum.com/showthread.php?threadid=53760


davesexcel[_102_]

Test if value is between two numbers
 

smplogc Wrote:
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/ex...html#Send_Mail

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


--
davesexcel


------------------------------------------------------------------------
davesexcel's Profile: http://www.excelforum.com/member.php...o&userid=31708
View this thread: http://www.excelforum.com/showthread...hreadid=537603


AA2e72E

Test if value is between two numbers
 
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


"smplogc" wrote:


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.


--
smplogc
------------------------------------------------------------------------
smplogc's Profile: http://www.excelforum.com/member.php...o&userid=33887
View this thread: http://www.excelforum.com/showthread...hreadid=537603



Edwin Tam[_7_]

Test if value is between two numbers
 
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

http://www.vonixx.com

"smplogc" wrote:


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.


--
smplogc
------------------------------------------------------------------------
smplogc's Profile: http://www.excelforum.com/member.php...o&userid=33887
View this thread: http://www.excelforum.com/showthread...hreadid=537603



Tom Ogilvy

Test if value is between two numbers
 
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

--
Regards,
Tom Ogilvy


"smplogc" wrote in
message ...

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.


--
smplogc
------------------------------------------------------------------------
smplogc's Profile:

http://www.excelforum.com/member.php...o&userid=33887
View this thread: http://www.excelforum.com/showthread...hreadid=537603





All times are GMT +1. The time now is 01:28 PM.

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