vba 取消输入框维护

vba inputbox maintenance on cancel

我正在寻找一种方法让输入框仅在输入整数 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

有什么建议或想法吗?

谢谢

试试这个:

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