VBA选择案例编号大于和小于

时间:2018-02-05 15:36:21

标签: vba

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

Case 0 to 7会检查a >= 0 and a <= 7。但我想要的是a >= 0 and a < 7

我也试过Case Is >=0, Is < 7

如何在Select Case

中执行此操作
  • a = 6.99999999应该会导致“ok”
  • a = 7应导致“否”

3 个答案:

答案 0 :(得分:2)

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

但是,除非您有两个以上的条件,否则我建议您改用If

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

我终于找到了解决方案。谢谢大家!