vba

VBA Select Case number to number greater than and less than


Sub ss()
  Dim a As Double
  a = 6.99999999
  Select Case a
    Case 0 To 7:
      MsgBox "ok"
    Case Else:
      MsgBox "no"
  End Select
End Sub

The Case 0 to 7 results in a check for a >= 0 and a <= 7. But what I want is a >= 0 and a < 7.

I also tried Case Is >=0, Is < 7.

How can I do this in a Select Case?


Solution

  • select case true
      case a >= 0 and a < 7
        MsgBox "ok"
      case else
        MsgBox "no"
    end select
    

    But, unless you have more than two conditions, I would suggest you use an If instead.