案例函数的更正

问题描述 投票:0回答:1

我正在尝试使用inputbox函数实现VBA代码。我用IF函数得到了结果,但我无法用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更正我的代码,我们非常感谢!

excel vba
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.