如何在Outlook VBA输入框中只允许输入数字

问题描述:

我在写一个宏,用户被要求输入一个数字。但我无法验证输入是否是数字。我正在尝试类型:= 1,但它给出了一个错误。这是我的一段代码。如何在Outlook VBA输入框中只允许输入数字

Limit = Replace(Trim(Left(Split(b, "LIMIT:")(1), 
     Len(Split(b, "LIMIT:")(1)) - 
     Len(Split(b, "EXCESS:")(1)) - 7)), ".", "") 
If Limit = "Up to full value any one Occurrence and in all 
      during the Period" & vbCrLf & vbCrLf & " " & vbCrLf & vbCrLf Then 
Limit = TIV 
Else 
Limit = InputBox(prompt:="Limit is not FULL VALUE. Enter Limit", 
     Title:="LIMIT", Default:=TIV,Type:=1) 
End If 
MsgBox Limit 

请建议这里应该是什么解决方案。

试试这个

Sub getLimit() 
    Dim Limit As Variant 
    Do While True 
     Limit = InputBox("please enter a number", "LIMIT") 
     If IsNumeric(Limit) Then 
      MsgBox Limit, , "LIMIT" 
      Exit Do 
     End If 
     If MsgBox("Limit is not FULL VALUE. Enter Limit", vbOKCancel, "LIMIT") = vbCancel Then Exit Do 
    Loop 
End Sub 
+1

您可以将_Limit = InputBox ...._行放在_do while_循环内,那么您不必在循环结束时重复它 – jsotola

+0

好的pt jsotola,我相应地优化 – curious

+0

@curious谢谢。这是工作。我相应地优化了它。 –

您需要使用Application.InputBox的类型的工作。

另一种方法是使用一个窗体包含一个文本框,该代码添加到Change()事件文本框中的:一旦

Private Sub TextBox1_Change() 
    With Me.TextBox1 
     If .Text Like "[!0-9]" Or Val(.Text) < -1 Or .Text Like "?*[!0-9]*" Then 
      Beep 
      .Text = Left(.Text, Len(.Text) - 1) 
     End If 
    End With 
End Sub 

您键入的字符会检查它是什么 - 如果它不是一个数字然后删除它。