Excel VBA 数値チェック高級版

  • デル株式会社
  • この間作った数値チェック自作関数をもうちょっと高級にした版。
    小数点とか符号とかチェックするようにしたよ。

    Public Enum DecimalType
        Digit = 0     '符号なし整数
        Sign = 1      '符号付き整数
        Deci = 2      '符号なし小数
        SignDeci = 3  '符号付き小数
    End Enum
    
    '  【機  能】 入力されている文字列は、「数値」かをチェックする
    '             DecimalTypeによって符号なし整数、符号付き整数、符号なし小数、符号付き小数を確認する
    '
    '  【形  式】 IsDigitDecimal
    '
    '  【入出力】 decimaltype: (I) Enum型の検査種別
    '             Value      : (I) 検査したい文字列
    '
    '  【戻り値】 数値か True:成功 False:失敗
    '
    Public Function IsDigitDecimal(ByVal dtype As DecimalType, ByVal value As String) As Boolean
        
        Dim ii As Long  'ループカウンタ
        Dim bDeci As Boolean    '小数フラグ
        
        '---------------------------
        '入力チェック
        '---------------------------
        If Len(value) = 0 Then
            Exit Function
        End If
        
        '---------------------------
        '初期化
        '---------------------------
        IsDigitDecimal = False
        bDeci = False
    
        '--------------------------------------------------------
        ' 実行部
        '--------------------------------------------------------
        For ii = 1 To Len(value)
            If ii = 1 Then
                If dtype = DecimalType.Sign Or dtype = DecimalType.SignDeci Then
                    '---------------------------
                    '符号付きの場合最初の1桁目が符号である事を確認
                    '---------------------------
                    If Mid(value, ii, 1) <> "+" And Mid(value, ii, 1) <> "-" Then
                        IsDigitDecimal = False
                        Exit Function
                    End If
                    '---------------------------
                    '小数の場合最初の1桁目が小数の場合NG
                    '---------------------------
                    If dtype = DecimalType.SignDeci Then
                        If Mid(value, ii, 1) = "." Then
                            IsDigitDecimal = False
                            Exit Function
                        End If
                    End If
                Else
                    '---------------------------
                    '符号なしの場合1桁目が文字はNG
                    '---------------------------
                    If Not Mid(value, ii, 1) Like "#" Then
                        IsDigitDecimal = False
                        Exit Function
                    End If
                End If
                '---------------------------
                ' どの種別の場合でも1桁目が小数点はNG
                '---------------------------
                If Mid(value, ii, 1) = "." Then
                    IsDigitDecimal = False
                    Exit Function
                End If
            '---------------------------
            'それ以外の場合
            '---------------------------
            Else
                '数字以外か?
                If Not (Mid(value, ii, 1) Like "#") Then
                    '---------------------------
                    '符号付き小数の場合、2桁目が文字はNG
                    '---------------------------
                    If ii = 2 And dtype = DecimalType.SignDeci Then
                        IsDigitDecimal = False
                        Exit Function
                    End If
                    
                    '---------------------------
                    '小数点が入ってる場合でもDecimalTypeが小数を許容しない場合はエラー
                    '---------------------------
                    If Mid(value, ii, 1) = "." And (dtype <> DecimalType.Deci And dtype <> DecimalType.SignDeci) Then
                        IsDigitDecimal = False
                        Exit Function
                    
                    '---------------------------
                    '小数点が入っていて、DecimalTypeが小数を許容する場合、小数点が複数入ってないか
                    '---------------------------
                    ElseIf Mid(value, ii, 1) = "." And (dtype = DecimalType.Deci Or dtype = DecimalType.SignDeci) Then
                        If bDeci = True Then
                            IsDigitDecimal = False
                            Exit Function
                        Else
                            '小数点が数値の最後でないか
                            If ii = Len(value) Then
                                IsDigitDecimal = False
                                Exit Function
                            End If
                        End If
                        bDeci = True
                    Else
                        IsDigitDecimal = False
                        Exit Function
                    End If
                    
                End If
            End If
        Next ii
        
        '全ての文字を検査した後、小数の場合、小数があったか確認
        If dtype = DecimalType.Deci Or dtype = DecimalType.SignDeci Then
            If bDeci = False Then
                IsDigitDecimal = False
                Exit Function
            End If
        End If
        IsDigitDecimal = True
    
    End Function
    
    

    こういう結果になる

        Dim ret As Boolean
        Debug.Print "debugAnswer:"
        Debug.Print "digittest"
       '整数
        ret = IsDigitDecimal(Digit, ""): Debug.Print ret
        ret = IsDigitDecimal(Digit, ".123"): Debug.Print ret
        ret = IsDigitDecimal(Digit, ".1.23"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "A123"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "123"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "123A"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "-123"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "123-"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "1.2.3"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "-1.2.3"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "1.23"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "12.3"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "123."): Debug.Print ret
        ret = IsDigitDecimal(Digit, "-1.23"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "-12.3"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "-123."): Debug.Print ret
        ret = IsDigitDecimal(Digit, "-A123"): Debug.Print ret
        ret = IsDigitDecimal(Digit, "-.123"): Debug.Print ret
        '符号整数
        Debug.Print "Signtest"
        ret = IsDigitDecimal(Sign, ""): Debug.Print ret
        ret = IsDigitDecimal(Sign, ".123"): Debug.Print ret
        ret = IsDigitDecimal(Sign, ".1.23"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "A123"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "123"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "123A"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "-123"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "123-"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "1.2.3"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "-1.2.3"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "1.23"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "12.3"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "123."): Debug.Print ret
        ret = IsDigitDecimal(Sign, "-1.23"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "-12.3"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "-123."): Debug.Print ret
        ret = IsDigitDecimal(Sign, "-A123"): Debug.Print ret
        ret = IsDigitDecimal(Sign, "-.123"): Debug.Print ret
        '小数
        Debug.Print "Decitest"
        ret = IsDigitDecimal(Deci, ""): Debug.Print ret
        ret = IsDigitDecimal(Deci, ".123"): Debug.Print ret
        ret = IsDigitDecimal(Deci, ".1.23"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "A123"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "123"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "123A"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "-123"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "123-"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "1.2.3"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "-1.2.3"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "1.23"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "12.3"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "123."): Debug.Print ret
        ret = IsDigitDecimal(Deci, "-1.23"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "-12.3"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "-123."): Debug.Print ret
        ret = IsDigitDecimal(Deci, "-A123"): Debug.Print ret
        ret = IsDigitDecimal(Deci, "-.123"): Debug.Print ret
        '符号小数
        Debug.Print "SignDecitest"
        ret = IsDigitDecimal(SignDeci, ""): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, ".123"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, ".1.23"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "A123"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "123"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "123A"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "-123"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "123-"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "1.2.3"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "-1.2.3"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "1.23"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "12.3"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "123."): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "-1.23"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "-12.3"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "-123."): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "-A123"): Debug.Print ret
        ret = IsDigitDecimal(SignDeci, "-.123"): Debug.Print ret
    

    結果

    debugAnswer:
    digittest
    False
    False
    False
    False
    True
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    Signtest
    False
    False
    False
    False
    False
    False
    True
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    Decitest
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    True
    True
    False
    False
    False
    False
    False
    False
    SignDecitest
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    True
    True
    False
    False
    False
    

    それなりにいいかんじ。
    123- とか、 .123 とか 123. とか、アメリカ式に許容したい場合は作りかえてね。



    関連記事

    ページ上部へ戻る