简单的VBA Select Case语句不起作用,为什么不呢?

时间:2015-06-16 17:00:23

标签: vba switch-statement case

Simple Select Case语句未按预期工作。消息应该显示为落入“太长”的桶,但它出现“太短”

Lenn = 20
    Select Case Lenn
    Case 7
        msg = "You've entered " & Lenn & " digits, this is ok"
    Case 11
        msg = "You've entered " & Lenn & " digits, is ok"
    Case 12
        msg = "You've entered " & Lenn & " digits, this is ok"
    Case 13
        msg = "You've entered " & Lenn & " digits, this is ok"
    Case 17
        msg = "You've entered " & Lenn & " digits, this is ok"
    Case Is < 7
        msg = "Hmm…. The number you entered is too short. Can you try again?"
    Case Is > 17
        msg = "Hmm…. The number you entered is too long. Can you try again?"
    Case Is = 8, 9, 10, 14, 15, 16
        msg = "Hmm…. The number you entered is not the right length. Can you try again?"
    Case Else
        msg = "Hmm…. The number you entered is not the right length. Can you try again?"
    End Select

1 个答案:

答案 0 :(得分:2)

如果Lenn是一个字符串,它将进入case语句的“太短”部分。尝试将其强制为整数,然后将其添加到代码顶部:

Dim Lenn As Integer