Accessing Excel workbook functions within the VBA environment
There are much fewer built-in functions in VBA than in the Excel workbook environment. Fortunately, you can access
the Excel workbook functions using the Application.WorksheetFunction statement.
The general format for this statement is,
Application.WorksheetFunction.functionname(arguments)
where functionname is the name of the function in the Excel workbook environment.
For example, let’s say you want to calculate the base-10 logarithm of a number. There is no built-in function in VBA
specifically for this task. However, there is a built-in function in the workbook environment, named Log10, that
calculates the base-10 logarithm of a number. We can access the Log10 function within VBA in the following
manner:
Option Explicit
Sub main()
Dim x As Double, ans as double
x = 100
ans = Application.WorksheetFunction.Log10(x)
MsgBox ans
End Sub
If you don’t know what a logarithm is, don’t worry about it. You won’t be tested on this concept. Just choose your
favorite Excel worksheet function.
On a side note, if you want to calculate the base-10 logarithm within VBA without using the Log10 workbook
function, you would have to use a combination of natural logarithm functions, Log.
Option Explicit
Sub main()
Dim x As Double
x = 10
ans = Log(x)/Log(10)
MsgBox ans
End Sub