大小写功能的更正

时间:2019-03-03 18:15:03

标签: excel vba

我正在尝试使用inputbox函数实现VBA代码。我可以通过IF-Function获得结果,但是我不能通过CASE-Function获得结果。

这是我的带有if函数的代码,我可以正确获得输出:

Sub test3()
Dim x As String

x = InputBox("What is your age?", "just a moment")

If x = "" Then
MsgBox "You did not answer the question!"
Else
    If IsNumeric(x) Then
        Range("A1").Value = "Your Age:"
        Range("B1").Value = x
    Else
        MsgBox "Please input no text"
    End If
End If
End Sub

我正在使用CASE-Function编写相同的代码,但没有任何输出:

Sub test2()
Dim x As String

x = InputBox("What is your age?", "Just a moment...")

Select Case x

Case Is = ""
   MsgBox "You did not answer the question!"
Case IsNumeric(x)
    Range("A1").Value = "You age:"
    Range("B1").Value = x
Case Else
    MsgBox "Please answer with a numerical value!"
End Select
End Sub

非常感谢您可以使用CASE-Function纠正我的代码!

1 个答案:

答案 0 :(得分:0)

建议使用Application.InputBox method (Excel)而不是InputBox function

InputBox method有一个data type参数,用于确定该方法可以接受的值。

尝试一下:

Sub Enter_Age()
Dim bAge As Byte, bTry As Byte

Enter_Age:
    bTry = 1 + bTry
    bAge = Application.InputBox("What is your age?", "just a moment", Type:=1)

    Select Case bAge
    Case False
        If bTry = 3 Then
            MsgBox "You did not answer the question!" & vbLf _
                & vbTab & "Process will be cancelled!"
                Exit Sub
        Else
            MsgBox "You did not answer the question!"
            GoTo Enter_Age
        End If

    Case Else
        Range("A1").Value = "Your Age:"
        Range("B1").Value = bAge

    End Select

    End Sub