Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between WorksheetFunction.IsNumber() and IsNumeric() in VBA?

Whats the different between using these two functions in VBA? Is there a speed advantage of using one over the other?

Application.WorksheetFunction.IsNumber(myVar)
IsNumeric(myVar)
like image 291
ChrisB Avatar asked Dec 02 '22 11:12

ChrisB


1 Answers

IsNumeric is a VBA standard library function located in the VBA.Information module, like IsArray, IsDate, IsError, Err, VarType, and more:

VBE Object Browser showing IsNumeric in the VBA standard library

As part of the VBA standard library, it works regardless of the host application: the standard library is safe to presume as existing on a machine that runs VBA.

Application.WorksheetFunction.IsNumber, on the other hand, is found in the Microsoft Excel object model library:

VBE Object Browser showing IsNumber in the Excel type library

As such, the code can only work if the Excel type library is referenced. If you're already in Excel, that's not a problem. But if you're in Word and you would like to use a function that returns a Boolean when the given String is numeric, you don't want to reference the whole Excel type library just for that.

The WorksheetFunction interface is a gateway to Excel's calculation engine: with it, you can write VBA code that can now use INDEX/MATCH against carefully crafted plain-VBA arrays - it's completely awesome. But there's no guarantee that Excel's calculation engine works exactly the same way VBA does. If you want to go by Excel's rules, use WorksheetFunction.

I would probably presume VBA.Information.IsNumeric to be faster than Excel.WorksheetFunction.IsNumber though: I could be wrong, but it seems like fewer moving parts are involved with just the VBA standard library.

But the only way to find out:

You have two horses - race them!

;-)

like image 84
Mathieu Guindon Avatar answered Dec 18 '22 20:12

Mathieu Guindon