Excel VBA:あるセルの内容が数字であるかを判定する自作関数

Excel VBA

サンプルコード

Public Function IsDoubleString(ByVal s As String, Optional ByVal result As Boolean = False) As Boolean

    If Not Len(s) = 0 Then
    
        Dim re As Object
        Set re = CreateObject("VBScript.RegExp")
        
        With re
        
            .Pattern = "^(0|-?[1-9]{1}[0-9]*){1}(\.[0-9]*)?$"    ' 検索パターンを設定
            .IgnoreCase = False                                  ' 大文字と小文字を区別する
            .Global = True                                       ' 文字列全体を検索
        End With
        
        If re.Test(s) Then
        
            result = True
        End If
    
        Set re = Nothing
    End If
    
    IsDoubleString = result
End Function

コメント