2

我在处理与输入框“取消”单击相关的错误时遇到问题。或者换句话说,如果输入的值为空,它会在 sub 内返回错误。我试过环顾四周,但似乎仍然无法完全正确。这是我的尝试:

Private Sub bttnSavingsExpected_Click()
Dim expected() As String
Dim nPeriods As Integer
Dim counter As Integer
Dim savings As Single

With ActiveSheet.Range("A13")
    nPeriods = Range(.Offset(1, 0), .End(xlDown)).Rows.Count
End With

ReDim expected(1 To nPeriods)

counter = 1

For counter = 1 To nPeriods
    expected(counter) = Range("A13").Offset(counter, 0).Value
Next

TryAgain:
On Error GoTo ErrH
counter = 1
For counter = 1 To nPeriods
    savings = InputBox("How much savings do you expect from " & expected(counter) & "?", "Savings?", Range("A13").Offset(counter, 1).Value)
    If savings = "" Then
        Exit Sub
    Else
    Range("A13").Offset(counter, 1).Value = savings
    End If
Next

Exit Sub
ErrH:
MsgBox "Please enter value. If the default value is desired then please click 'OK'.", vbOKOnly, "Do Not Click Cancel"
GoTo TryAgain

End Sub

通过这种尝试,无论是否有输入,第一次单击都会显示 MsgBox,即使我单击“确定”也是如此。单击“确定”或“取消”的第二次尝试导致被踢回编辑器。

4

2 回答 2

3

你有Dim savings As SingleIf savings = "" Then。那总是会出错

尝试使用Dim savings As Variant

于 2013-05-01T10:59:23.317 回答
0

确保收件箱的变量设置为“”,然后测试 False 的值。比我见过的任何东西都容易:

Sub WolfPackURL_input()
Dim TheURL As String
Dim SaveURL As Hyperlink
Set savedURL = Sheets("Data").Range("I1")

TheURL = ""
TheURL = Application.InputBox("Input the Sign-Up URL", "Wolfpack Weekly Players URL", "http://something", 1)

If TheURL = "False" Then
    Exit Sub
End If

ThisWorkbook.Worksheets("Data").Activate
Sheets("Data").Range("I1").Hyperlinks.Delete
Sheets("Data").Range("I1").ClearContents
Sheets("Data").Range("I1").Clear

ActiveSheet.Hyperlinks.Add anchor:=Sheets("Data").Range("I1"), Address:=TheURL, ScreenTip:="Open file", TextToDisplay:=TheURL

End Sub
于 2020-04-13T23:28:27.527 回答