Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different results using IsNumber Function on Date in VBA

Say cell A1 has a Date: 1/1/2017.

Then the macro below gives different results:

  • first - True
  • second - False.

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
like image 533
Nicholas Avatar asked Apr 29 '17 13:04

Nicholas


People also ask

Does Isnumber work with dates?

ISNUMBER will return TRUE for Excel dates and times since they are numeric.

Can you use Isnumber in VBA?

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.

What does IsNumeric mean in VBA?

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.

How do I use ISNUMBER in VBA?

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 ...

What is the difference between isnumeric and ISNUMBER in Excel?

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.

How to check if a value is numeric in VBA?

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.

What is the difference between VBA date and Excel date?

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.


1 Answers

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.

like image 171
A.S.H Avatar answered Oct 01 '22 11:10

A.S.H