Forum Discussion
Data Types in Excel
- 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.
Excel does not directly expose a VBA object or property that provides information about the data types of cells in a worksheet, as far as I know. However, you can use VBA to loop through the cells in a range and infer their data types based on their values.
Below is a simple VBA code that you can use to print the data types of cells in a specified range:
Vba code (is untested):
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 IsBoolean(cellValue) Then
GetDataType = "Boolean"
Else
GetDataType = "Text"
End If
End Function
This code defines a subroutine (PrintDataTypes) that loops through each cell in the specified range, prints the cell address, and calls a helper function (GetDataType) to determine the data type of the cell value.
Note: This approach provides a basic categorization of data types. It may not cover all edge cases, and it treats anything that is not numeric, date, boolean, or empty as text.
To run the code, press Alt + F8, select PrintDataTypes, and click Run.
Keep in mind that if your worksheet has a large number of cells, this operation might take some time to complete. Also, remember to replace "Sheet1" with the actual name of your worksheet and adjust the range accordingly. The text and steps were edited with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.
- anupambit1797Nov 26, 2023Iron Contributor
- NikolinoDENov 27, 2023Gold 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