3

私はVBAで立ち往生しています。ウェブサイトで他の解決策を試しましたが、まだうまくいきません。複数のモジュールとフォームを使用して、Excel のセルに情報を入力しています。しかし、msgBox を空白のままにすると、タイプ 13 の不一致エラーが発生します。isNull を試してみましたが、使い方がよくわかりません。

私はせいぜい初心者のプログラマーであるため、回答はできるだけ単純にしてください。ありがとう

Sub GetEndCostGateFees()
    Dim qtyGateFees As Double
    Dim msg As String

Const MinCost As Integer = 0
Const MaxCost As Integer = 200

msg = "Please enter the cost, per tonne, of Gate fees "

Do
    qtyGateFees = InputBox(msg, "Gate Fees")

    If IsNull(qtyGateFees) Then
        MsgBox ("Please enter a value. Enter 0 if none")
        End If

    If IsNumeric(qtyGateFees) Then
        If qtyGateFess >= MinCost And qtyGateFees <= MaxCost Then Exit Do
        End If
        msg = "Please enter a valid number"
        msg = msg & vbNewLine
        msg = msg & "Please enter number between " & MinCost & " and " & MaxCost
        Loop

Sheet25.Range("B43").Value = qtyGateFees

サブ終了

4

3 に答える 3

4

ユーザーに数値入力のみを入力させたい場合は、 with を使用Application.InputBoxしますType:=1

Sub sample()
    Dim Ret As Variant
    Dim msg

    msg = "Please enter the cost, per tonne, of Gate fees "

    Ret = Application.InputBox(msg, "Gatefees", Type:=1)

    If Ret <> False Then
        '
        '~~> Rest of your code here
        '
    End If
End Sub
于 2013-10-17T14:02:45.643 に答える
0

qtyGateFees を Variant に変更します。

Dim qtyGateFees As Variant

「Double」として淡色表示されている変数に空白の値を割り当てようとしているため、型の不一致エラーが発生していると思います。

次に、isNull ではなくこれを試すことができます。

If qtyGateFees = "" Then
于 2013-10-17T13:58:14.977 に答える
0

変数を として宣言する代わりに、Variantエラー処理を使用できます (これはとにかく良い方法です)。

Option Explicit
    Sub GetEndCostGateFees()
        Dim qtyGateFees As Double
        Dim msg As String

        Const MinCost As Integer = 0
        Const MaxCost As Integer = 200

        msg = "Please enter the cost, per tonne, of Gate fees "

        Do
            On Error GoTo TypeM
            qtyGateFees = InputBox(msg, "Gate Fees")
            On Error GoTo 0

            If IsNumeric(qtyGateFees) Then
                If qtyGateFees >= MinCost And qtyGateFees <= MaxCost Then Exit Do
            End If
            msg = "Please enter a valid number"
            msg = msg & vbNewLine
            msg = msg & "Please enter number between " & MinCost & " and " & MaxCost
        Loop

        Sheets(Sheet25).Range("B43").Value = qtyGateFees

    Exit Sub

        TypeM:
        If Err.Number = 13 And Err.Description = "Type mismatch" Then
            MsgBox "Please enter a value. Enter 0 if there were no fees."
            Err.Clear
            Resume
        Else
            Debug.Print Err.Number & " " & Err.Description
        End If

    End Sub
于 2013-10-17T14:15:03.150 に答える