Forum Discussion

anupambit1797's avatar
anupambit1797
Iron Contributor
Nov 26, 2023
Solved

Data Types in Excel

Dear Experts,                  Is there some VBA code, which can print all the "Data Types"  in Excel   Data Types **************** Size in bytes **************Range Bytes Integer Signed Intege...
  • NikolinoDE's avatar
    NikolinoDE
    Nov 27, 2023

    anupambit1797 

    Maybe you can use the VarType function to check.

    Sub PrintDataTypes()
        Dim ws As Worksheet
        Dim rng As Range
        Dim cell As Range
        
        ' Set the worksheet and range
        Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name
        Set rng = ws.UsedRange ' Change to your desired range
        
        ' Loop through each cell in the range
        For Each cell In rng
            Debug.Print "Cell (" & cell.Address & ") Data Type: " & GetDataType(cell.Value)
        Next cell
    End Sub
    
    Function GetDataType(cellValue As Variant) As String
        ' Function to determine the data type of a cell value
        If IsEmpty(cellValue) Then
            GetDataType = "Empty"
        ElseIf IsNumeric(cellValue) Then
            GetDataType = "Number"
        ElseIf IsDate(cellValue) Then
            GetDataType = "Date"
        ElseIf VarType(cellValue) = vbBoolean Then
            GetDataType = "Boolean"
        Else
            GetDataType = "Text"
        End If
    End Function

     Vba code is untested.

Resources