Using Worksheet Functions in a Procedure

Many worksheet functions can be used directly in Visual Basic procedures; the IF function is one exception, though, as If is also a keyword in Visual Basic. You can use the Application qualifier to run a Microsoft Excel worksheet function rather than a Visual Basic function. For example, the following procedure causes a "Sub or Function not defined" error because it doesn't identify ACOS as a worksheet function.


Sub MissingObject()
    x = Acos(-1)
End Sub

The following procedure successfully uses the Microsoft Excel worksheet function ACOS because the code first refers to the Application object.


Sub ReturnArccosine()
    x = Application.Acos(-1)
End Sub

The only worksheet function that requires you to explicitly specify that you're referring to either the function's Microsoft Excel version or its Visual Basic version is the LOG function, because both function names are spelled the same way. The Microsoft Excel LOG function returns the logarithm of a number to the base you specify. The Visual Basic Log function returns the natural logarithm of a specified number.