Returns the natural logarithm of a number.
Simple Log Examples
Sub Abs_Example() MsgBox Log(100) End Sub
This code will return 4.60517018598809
In the VBA Editor, you can type “Log(” to see the syntax for the Log Function:
The Log function contains an argument:
Number: Any valid numeric expression greater than zero.
Examples of Excel VBA Log Function
All the above examples are cases with the base e(the base of the natural logarithm).
To calculate the logarithm with any base, you can use the code as following function.
Function LogAny(b As Double, x As Double) As Double ' b: base x: a given number LogAny = Log(x) / Log(b) End Function Sub Log_Example1() MsgBox LogAny(10, 100) End Sub
The result will be as following.
VBA Coding Made EasyStop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!