PDA

View Full Version : [SOLVED] Between two numbers



ksilenio
11-12-2019, 01:43 PM
Hi to all . I want to write a statement with an " if " between two numbers , how can it be done? eg If 150 between 100 and 200 then ... "do something" end if
Regards

snb
11-12-2019, 02:01 PM
In A1: 30
In A2:200
In A3:100


=INT(A1/(A2-A3))=0

ksilenio
11-12-2019, 02:12 PM
Hi thank you for your answer but i don't understant , how can i use this in the VBA code , what i want is how to syntax the "if" statement

p45cal
11-12-2019, 04:36 PM
If number1 >100 and number1 < 200 then
'...do whatever you want done
End Ifor on one line:

If number1 >100 and number1 < 200 then '...do somethingwhere number1 is a variable containing your number (150 in your example).

snb
11-13-2019, 03:15 AM
Sub M_snb()
x = 99
y = 200
Z = 100
If Int((x - Z) / (y - Z)) = 0 Then MsgBox x & " lies between " & Z & " and " & y
End Sub

ksilenio
11-13-2019, 12:04 PM
OK Thank you

Paul_Hossler
11-16-2019, 10:23 PM
Personal style

1. I find it's better if I use parentheses to group each part of the AND

2. I like to arrange the conditionals is what to me is a logical order based on the way I think

L <= N <= H

3. I don't see any reason to squeeze everything onto one line since I find it can make things harder to read

Just seems to make it easier for me





Sub test()
Dim n As Long, L As Long, H As Long


n = 150
L = 100
H = 200

If (L <= n) And (n < H) Then
MsgBox n & " is between " & L & " and " & H
Else
MsgBox "nope"
End If


End Sub





If you had to do a lot, you could make a boolean function. It's simple, but might make it easier to follow the logic. Probably costs a few insignificant CPU cycles



Sub test2()
Dim N As Long, L As Long, H As Long


N = 150
L = 100
H = 200

If Between(L, N, H) Then
'Do Something
End If


End Sub




Function Between(L As Variant, N As Variant, H As Variant) As Boolean
Between = (L <= N) And (N < H)
End Function