Application.InputBox Method
では、返されるデータの種類を指定できます。
![enter image description here](https://i.stack.imgur.com/OMdqy.png)
Sub Example1()
Dim number As Integer
number = Application.InputBox(Prompt:="Enter an integer:", Type:=1)
End Sub
MSDN Application.InputBox Method (Excel)
ユーザーがキャンセルした場合、0を返します。タイプ1のパラメータと
Application.InputBox
ので、私は標準
InputBox
を使用することを好むだろう。それを使用する方法は、別の変数で値を取得し、戻り値が条件を満たすことをテストすることです。このようにして、エラーを回避できます。
Sub Example2()
Dim number As Integer
Dim result As String
result = InputBox("Enter an integer:")
If result = "" Then
MsgBox "Good Bye", vbInformation, "Action Cancelled"
Exit Sub
ElseIf IsNumeric(result) Then
If CDbl(result) > CInt(result) Then
MsgBox "You entered a Decimal" & vbCrLf & "Try Again", vbInformation, "Intergers Only"
Else
number = result
End If
Else
MsgBox "Intergers Only" & vbCrLf & "Try Again", vbInformation, "Intergers Only"
End If
End Sub
エラーの前に 'exit sub'を追加してください: –
エラーメッセージをスキップするメカニズムがないので、常に表示されます。 – Rory
もう1つの提案 - あなたの名前を使用するときは、予約語や予約語を避けるようにしてください。したがって、 'error'ラベルの代わりにかなり一般的な' ErrorHandler'を使うことができます。 –