Home » excel » excel – The variable belongs to a range of values

excel – The variable belongs to a range of values

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have this code:

If t=241 Then 

...

End if 

But if i want the variable to be equal to the range of values ​​(241-540), how to write it?

How to&Answers:

If your condition start to become more complicated, you might want to take a look at the Select Case statement:

Select Case t
Case 241 To 540, 620 To 728 
    'Code executed for values to t between 241 and 540, or between 620 and 728
    'similar to If (t >= 241 And t <= 540) Or (t >=620 And t <= 728) Then
Case 800
    'Code executed if t=800
Case Else
    'Other case
End Select

Answer:

This is what you’re looking for:

If t > 241 And t < 540 Then

'...

End if