# How to Use LOG Function in Excel VBA (5 Suitable Examples)

Get FREE Advanced Excel Exercises with Solutions!

The VBA LOG function is a built-in Math or Trig function in Excel. The LOG function is available in both Excel Worksheet and VBA however they differ by syntax. In this article, we discuss and demonstrate the VBA LOG function and its usage.

## Excel VBA LOG Function: Syntax and Arguments

ðŸ”„ Function Objective:

Â To find the logarithm value of any given numeric expression greater than zero (0).

ðŸ”„ Syntax:

The syntax of the VBA LogÂ function is

`Log(number)`

ðŸ”„ Arguments Explanation:

Argument Required/Optional Explanation
number Required any numeric expression greater than zero (0)

ðŸ”„ Return Parameter:

The VBA LOG function returns a double (i.e., decimal negative or positive value type) or integer type numeric value.

ðŸ”„ Applies To:

Microsoft Excel Version 2003, Excel 2011 for Mac and Onwards.

â§¬ Note

VBA Log function works as a natural logarithm with a base e. So, the e-based logarithm returns values approximately 2.303 times of 10 based logarithm. If you want to calculate the n-based logarithm of a number (i.e.,x), we have to divide the natural logarithm of that number (i.e., logex) by the natural logarithm of the base (i.e., logen). As a result, the formula becomes,

`Log``n``x= (log``e``x / log``e``n)`

## â§­ Opening and Inserting Module in Microsoft Visual Basic

Before proceeding to demonstrate any examples, itâ€™s necessary to know the ways to open and insert a Module in Microsoft Visual Basic in Excel.

ðŸ”„ Opening Microsoft Visual Basic: There are mainly three ways to open the Microsoft Visual Basic window,

ðŸ”¼ Using Keyboard Shortcuts

Â Press ALT+F11 altogether to open the Microsoft Visual Basic window.

ðŸ”¼ Using Developer Tab

Â In an Excel worksheet, Go to Developer Tab > Select Visual Basic. The Microsoft Visual Basic window appears.

ðŸ”¼ Using Worksheet Tab

Go to any worksheet, Right-Click on it > Choose View Code (from the Context Menu).

ðŸ”„ Inserting a Module in Microsoft Visual Basic: There are two ways to insert a Module in Microsoft Visual Basic window,

ðŸ”¼ After opening the Microsoft Visual Basic window, Select a Worksheet > Right-Click on it > Select Insert (from the Context Menu) > then Choose Module.

ðŸ”¼ You can also do it by Selecting Insert (from the Toolbar) > then Choosing Module.

## 1. Using VBA LOG Function to Return a Logarithmic Value

From the syntax, we know that the VBA LOG function requires only one argument in numeric form. In this example, we simply use the LOG function to return a logarithmic value of a given number (i.e., 1000). We provide the number in the function and take the resultant value in a message box. To achieve the logarithmic value in a message box execute the below sequences.

âž¤ Paste the following macro code in a Worksheet Code of Microsoft Visual Basic window,

``````Sub Logarithm_Value()
MsgBox Log(1000)
End Sub``````

âž¤ in the code,

1 â€“ start the macro procedure by declaring the Sub name. You can assign any name to the code.

2 â€“ display the logarithm value (i.e., 1000) in a message box.

âž¤ Press F5 to run the Macro and it brings out a message box depicting the functionâ€™s outcome.

In the above screenshot, we show values provided by the Worksheet LOG function and VBA LOG function to differentiate them.

## 2. Calculating N-Based Logarithmic Value Using VBA LOG Function

Now, we know that the VBA LOG function calculates logarithmic values using the e-based natural logarithm. If we have to calculate the logarithmic value with a different base, we can use the previously discussed (i,e., in the Note section) formula

`Lognx= (logex / logen)`

âž¤ Write the following code in the Microsoft Visual Basic window.

``````Function LogRND(N As Double, X As Double) As Double
LogRND = Log(X) / Log(N)
End Function
Sub Log_N_Based()
MsgBox LogRND(10, 1000)
End Sub``````

Â

âž¤ From the above image, in the sections,

1 â€“ begin the macro code by declaring a function name and variables.

2 â€“ assign a formula to find any n-based logarithm values.

3 â€“ give the performing macro code a name.

4 â€“ the logarithm value shows up in a message box. Also assign the base(i.e.,10) and value (i.e., 1000).

âž¤ Hit F5 to run the macro and it displays a message box with the resultant value in it as depicted in the following image.

You can see the resultant value of the Worksheet LOG function and the VBA LOG function is the same. Itâ€™s because in both cases we use a 10 based logarithm.

## 3. Finding Multiple Logarithm Values (Result in the Immediate Window)

In earlier examples, we calculate logarithmic values for a single given number. We can calculate multiple logarithmic values of multiple given numbers. And we can display them in the message box or immediate window. For this example, we display multiple logarithmic values in the immediate window below the code window.

âž¤ Use the below code in any Microsoft Visual Basicâ€™s Sheet code window.

``````Option Explicit
Sub Multiple_Values()
Dim Value1 As Double
Dim Value2 As Double
Dim Value3 As Double
Value1 = Log(1)
Value2 = Log(0.05)
Value3 = Log(100)
Debug.Print Value1
Debug.Print Value2
Debug.Print Value3
End Sub``````

âž¤ The codeâ€™s sections,

1 â€“ initiate the macro procedure declaring the Sub name.

2 â€“ declare the variables Value1, Value2, and Value 3 as Double. As we mentioned earlier, the return values will be in any numeric value (i.e., negative or positive decimal or integer), we declare the variables as double.

3 â€“ assign the values to variables. Where Value1 = Log(1), Value2 = Log(0.05), and Value3 = Log(100). You can use any numeric value except zero (0) and negative (â€“).

4 â€“ print the resultant logarithm value in the Immediate window below the Sheet code window.

âž¤ Use F5 to run the macro and it brings out in the immediate window as shown in the following image.

âž¤ You can compare the VBA LOG functionâ€™s resultant values to the Worksheet LOG functionâ€™s outcomes depicted in the below picture.

## 4. Calculating Multiple Logarithmic Values from Cell Reference

In this example, we use cell references to insert a value into the formula and to display the resultant values in certain cells.

Before that, we calculate the logarithmic values of three given values (i.e., 1,0.05, and 500) using Worksheetâ€™s LOG function. And keep a column to insert the resultant values using the VBA LOG function.

âž¤ Type the following code in a Sheetâ€™s code of Microsoft Visual Basic window. Â

``````Option Explicit
Sub VBA_LOG_Function_Cell_Reference()
Range("C3").Value = Log(Range("B3"))
Range("C4").Value = Log(Range("B4"))
Range("C5").Value = Log(Range("B5"))
End Sub``````

âž¤ From the above image, the codeâ€™s sections,

1 â€“ take forward the macro by setting the Sub name.

2 â€“ assign the values to the formula (i.e., =Log()) with cell reference (i.e., B3 or B4) as an argument. And display the resultant value in certain cells (i.e., C3 or C4). You can use any cell as a cell reference.

âž¤ Press F5 to run the Macro and it makes the logarithm values appear in referenced cells as shown in the following picture.

Related Content: How to Use VBA Val Function in Excel (7 Examples)

## 5. Calculating Logarithmic Values from Range

We show how to fetch, and display given values and resultant values in a certain cell in the previous example (i.e., Example 4). In this example, weâ€™ll take that example a step further. We use a range (i.e., B3:B5) to insert given values and the VBA OFFSET function to display logarithmic values in cells.

Prior to the VBA LOG functionâ€™s execution, we calculate the logarithmic values of the given value using Worksheetâ€™s LOG function.

âž¤ Paste the following code in a Sheetâ€™s Code window.

``````Sub Logarithm_Value_Range()
Dim i As Range
For Each i In Range("B3:B5")
i.Offset(0, 1) = Log(i)
Next i
End Sub``````

âž¤ The code is marked in parts,

1 â€“ begin the macro by setting the Sub name.

2 â€“ declaring the variable as Range.

3 â€“ assign range value (i.e., B3:B4) for each iteration.

4 â€“ create a loop and display the value in cell position at the end of each iteration.

âž¤ Push the F5 key to run the Macro and it displays the logarithmic values in destined cells.

Related Content: How to Use VBA Round Function in Excel (6 Quick Uses)

## â§­ Things to Keep in Mind

ðŸ”„ The syntax of the Excel Worksheet LOG function is `LOG (number, [base]) `and the VBA LOG function is `Log(number)`.

ðŸ”„ Excel Worksheetâ€™s LOG function calculates logarithm values 10-based by default and the VBA LOG function works as natural logarithm (i.e., e-based).

## Conclusion

In this article, we discuss and demonstrate the uses of the VBA Log function. We try to clear the basics and cover typical uses of this function. I hope the above-described uses of the VBA Log function intrigue you to use the function more efficiently. If you have further queries or feedback, please let me know in the comment section. See you in my next article.

## Related Articles

Maruf Islam

My self, Maruf Islam, an engineer and Excel & VBA Content developer on Exceldemy. I enjoy solving problems, finding workable solutions, and most of the part I really like to take on challenges. On Exceldemy I write articles discussing various way outs of Microsoft Excel's stuck ons.

We will be happy to hear your thoughts

Advanced Excel Exercises with Solutions PDF