Say cell A1 has a Date: 1/1/2017.
Then the macro below gives different results:
What is the reason of this difference? How should I use IsNumeric correctly?
Sub TestIsNumber()
MsgBox WorksheetFunction.IsNumber(Cells(1, 1))
MsgBox WorksheetFunction.IsNumber(Cells(1, 1).Value)
End Sub
ISNUMBER will return TRUE for Excel dates and times since they are numeric.
The ISNUMERIC function is a built-in function in Excel that is categorized as an Information Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.
Introduction to IsNumeric The IsNumeric VBA function checks if a cell is a number and expresses the answer as a Logical Boolean ( True or False ). The IsNumeric VBA function is a counterpart to the Excel ISNUMBER function, but the expressions don't always produce the same results.
Using IsNumber in VBA. IsNumber is the Excel function, which can be used in VBA also and has an almost similar output as IsNumeric. Let’s look at the example of the IsNumber function: If Application.WorksheetFunction.IsNumber (Sheet1.Range ("A1").Value) = True Then MsgBox "The value in A1 is numeric" Else MsgBox "The value in A1 is not ...
Both functions return TRUE if the cell contains a number and FALSE, if not. However, the Excel ISNUMERIC function is a VBA function whereas the ISNUMBER function is a worksheet function. Both functions can also yield different results in similar circumstances.
IsNumeric is the VBA function which checks if a value is numeric and returns a Boolean TRUE or FALSE as a result. The function can take a variable or a cell value.
Things to Remember. VBA DATE function returns the current system date and as parallel to Excel’s TODAY () function. VBA DATE function does not have any argument to be passed in excel. Doesn’t even need the parentheses to be called while using this function in the code. VBA DATE is a non-volatile function in excel.
It's .Value
vs .Value2
When the cell is formatted as date, .Value
converts it to a Variant/Date
datatype, while .Value2
returns its embedded numeric number (Variant/Double
). .Value2
either returns a string, a number or an error variant. It doesn't convert to date or currency.
.Value2
is the recommended way to read cell values, use it to always have the numeric representation of dates, which is faster and still permits you to manipulate it in VBA (compare it to another date, convert it back to Date
datatype, etc..).
Notice that the version WorksheetFunction.IsNumber(Cells(1, 1))
invoked the Array version
of Excel's IsNumber
(It's similar to function overloading in C++, and the best matched overload is invoked). Hence the Range Object
was sent to the function as parameter, not its .Value
(implicit call to .value
did not happen here). Excel worked on the embedded .Value2
as if you type =IsNumber(A1)
in another cell, which returns TRUE
in Excel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With