【问题标题】:vba inputbox maintenance on cancelvba 输入框维护取消
【发布时间】:2020-07-29 20:40:43
【问题描述】:

我正在寻找一种仅在输入整数 1-12 时让输入框通过的方法。 其中的任何字符串,双值,空或 ESCAPE(取消)按钮都会抛出 Exit Sub

我知道网上有很多例子,我都试过了!但总是在 ESCAPE 或其他场合出现错误。

到目前为止我有这个,但它在取消时出错:

dMonth = InputBox("Which month to count?", "Choose month", Format(Date, "m") - 1)
If (Not (Int(dMonth) >= 0 And Int(dMonth) <= 12)) Or StrPtr(dMonth) = 0 Or StrPtr(dMonth) = 698279968 Or dMonth = "" Then Exit Sub

有什么建议或想法吗?

谢谢

【问题讨论】:

  • 假设 dMonth 是一个字符串,因为这是 InputBox 返回的,Int 将遇到空字符串的问题。请改用Val
  • 最初我使用整数,但问题太多,所以我尝试使用字符串作为所有 google sggests。请给出代码示例,如果它可以完全工作,我可以标记为正确答案

标签: vba inputbox


【解决方案1】:

试试这个:

Private Sub Command1_Click()
   Dim sMonth As String
   Dim dMonth As Double

   sMonth = InputBox("Which month to count?", "Choose month", Format(Date, "m") - 1)
   dMonth = Val(sMonth)                         'convert user input to numeric
   If dMonth <> Fix(dMonth) Then Exit Sub       'check for an integer
   If dMonth < 1 Or dMonth > 12 Then Exit Sub   'check for required range

   MsgBox dMonth
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2016-04-29
    • 1970-01-01
    相关资源
    最近更新 更多