2017-07-16 145 views
1

我在寫一個宏,用戶被要求輸入一個數字。但我無法驗證輸入是否是數字。我正在嘗試類型:= 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 

請建議這裏應該是什麼解決方案。

回答

1

試試這個

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謝謝。這是工作。我相應地優化了它。 –

1

您需要使用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 

您鍵入的字符會檢查它是什麼 - 如果它不是一個數字然後刪除它。

相關問題