Thursday, June 11, 2020

Getting data type from given value whether Number/String/Date/Alphanumeric

Hi Folks,

Sometimes need to generate some random test data based on given value of cell or variables data value, whether it is number, date, string, alphabets.
strOrignalData="John Smith" 'DOB,phone number any original data

 sDataType = GetDatatype(strOrignalData)

Public Function GetDatatype(strData)
            numbers = 0
            alphabets = 0
            specialChar = 0
            For i = 1 To Len(strData)
                b = Mid(strData, i, 1)
                If IsNumeric(b) Then
                 numbers = numbers + 1
                ElseIf (Asc(b) >= 97) And (Asc(b) <= 122) Or Asc(i) >= 65 And Asc(i) <= 90 Then
                 alphabets = alphabets + 1
                ElseIf InStr(b, "/") > 0 Then
                 specialChar = specialChar + 1
                Else
                    'None
                End If
            Next
            If numbers = Len(strData) Then
                strdatatype = "Numbers"
            ElseIf specialChar >= 2 Then
                strdatatype = "Date"
            ElseIf alphabets > 0 And numbers = 0 Then
                strdatatype = "Alphabets"
            Else
                strdatatype = "Alphanumeric"
            End If
            GetDatatype = strdatatype
         
End Function

cheers.
TJ

1 comment: