【发布时间】:2019-06-28 16:47:39
【问题描述】:
我有一个非 VBA 程序,它从由循环激活的一系列 InputBox 中获取用户输入。我需要它,这样,如果用户单击“取消”按钮,它就会跳出循环。不幸的是,“取消”按钮没有这样做,而是返回一个空字符串,然后将其传递给我的输入验证函数,失败,并给出一个失败的验证消息。在验证消息上单击“确定”会再次弹出 InputBox。这将创建一个取消 > 确定 > 取消 > 确定的无限循环。我需要它工作,以便当用户单击“取消”时,InputBox 关闭而不发送任何输入。这可能吗?感谢您的帮助:)
这是我的代码:
'Assign value to variable
strAmountInput = InputBox("Please enter the monthly rainfall for " & strMonth)
If Validation(strAmountInput) = True Then
'Set Value for dblAmount
dblAmount = CDbl(strAmountInput)
'Add monthly rainfall amount to array, using i as index
dblMonthlyRain(i) = dblAmount
'Add item displaying monthly rainfall to listbox
lstMonthlyRainfall.Items.Add("Rainfall for " & strMonth & " = " & CStr(dblMonthlyRain(i)))
Else
'Prevent loop from advancing if validation fails, forces user to enter valid input before moving on
i -= 1
End If
_______________________________________________________________________________________
Private Function Validation(Amount As String) As Boolean
'Validate user input for InputBox
If IsNumeric(Amount) = False Then
MessageBox.Show("Please enter numbers only")
Return False
Else
Return True
End If
End Function
【问题讨论】:
-
If String.IsNullOrEmpty(strAmountInput) Then Exit Do或Exit For或Exit While。也许,摆脱相当丑陋的 InputBox 并构建一个专门用于此任务的表单。