Forum Discussion
anupambit1797
Nov 26, 2023Iron Contributor
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...
- Nov 27, 2023
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.
anupambit1797
Nov 26, 2023Iron Contributor
NikolinoDE
Nov 27, 2023Platinum Contributor
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.
- anupambit1797Nov 27, 2023Iron Contributor
Thanks NikolinoDE , I used Matlab to get this, for now purpose is fulfilled with Matlab.. Thanks anyways 🙂
Thanks & Regards
Anupam Shrivastava