数値チェック(IsNumeric)@Excel マクロ・VBA

IsNumeric関数を用いて、数値チェックが可能です。

IsNumeric(expression)
引数のexpresionには、数値や文字列を指定します。

引数が数値の場合は、Trueを返します。
数値以外は、Falseです。



Sub test()

'********以下は、数値として認識される********

    If IsNumeric(111) Then
        MsgBox "数字です。"
    Else
        MsgBox "数字ではないです"
    End If


    If IsNumeric(1.11) Then
        MsgBox "数字です。"
    Else
        MsgBox "数字ではないです"
    End If



'********以下は、数値として認識されない********

    If IsNumeric("ああ") Then
        MsgBox "数字です。"
    Else
        MsgBox "数字ではないです"
    End If


    If IsNumeric("aa") Then
        MsgBox "数字です。"
    Else
        MsgBox "数字ではないです"
    End If


    If IsNumeric("2008/10/10") Then
        MsgBox "数字です。"
    Else
        MsgBox "数字ではないです"
    End If


End Sub




Copyright (C) Excelマクロ・VBAのお勉強. All Rights Reserved.