Excel VBA Worksheet Functions
Worksheet function in VBA is used when we have to refer to a specific worksheet, normally when we create a module the code executes in the currently active sheet of the workbook but if we want to execute the code in the specific worksheet we use worksheet function, this function has various uses and applications in VBA.
The best thing about VBA is, like how we use formulas in worksheet similarly, VBA too has its own functions. If this is the best, then it has a beautiful thing as well. That is “we can use worksheet functions in VBA as well.”
Yes!!! You heard it right; we can access to worksheet functions in VBA also. We can access some of the worksheet functions while writing the code and make it part of our code.
How to use Worksheet Functions in VBA?
In the worksheet, all the formulas start with equal (=) sign, similarly in VBA coding, in order to access worksheet formulas, we should use the word “WorksheetFunction.”
Before you enter any worksheet formula, you need to mention the “WorksheetFunction” object name then put a dot (.), then you will get a list of all the available functions under this object.
In this article, we will exclusively concentrate on how to use worksheet functions in VBA codingVBA CodingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task., which will add more value to your coding knowledge.
#1 – Simple SUM Worksheet Functions
Ok, to start off with worksheet functions, apply the simple SUM function in excel to add numbers from the worksheet.
Assume you have monthly sales & cost data in the worksheet like the below one.
In B14 and C14, we need to arrive at the total of the above numbers. Follow the below steps to start the process of applying the “SUM” function in Excel VBA.
Step 1: Create a simple, excel macro name.
Sub Worksheet_Function_Example1() End Sub
Step 2: Since we need the result in cell B14, start the code as Range (“B14”).Value =
Sub Worksheet_Function_Example1() Range("B14").Value = End Sub
Step 3: In B14, we need the value as the result of the sum of the numbers. So in order to access the SUM function from the worksheet, start the code as “WorksheetFunction.”
Sub Worksheet_Function_Example1() Range("B14").Value = WorksheetFunction. End Sub
Step 4: The moment you put a dot (.), it will start to display the functions available. So select SUM from this.
Sub Worksheet_Function_Example1() Range("B14").Value = WorksheetFunction.Sum End Sub
Step 5: Now give the reference of the above numbers, i.e., Range (“B2:B13”).
Sub Worksheet_Function_Example1() Range("B14").Value = WorksheetFunction.Sum(Range("B2:B13")) End Sub
Step 6: Similarly, for the next column, apply a similar code by changing the cell referencesCell ReferencesCell reference in excel is referring the other cells to a cell to use its values or properties. For instance, if we have data in cell A2 and want to use that in cell A1, use =A2 in cell A1, and this will copy the A2 value in A1..
Sub Worksheet_Function_Example1() Range("B14").Value = WorksheetFunction.Sum(Range("B2:B13")) Range("C14").Value = WorksheetFunction.Sum(Range("C2:C13")) End Sub
Step 7: Now run this code manually or using the F5 key to have a total in B14 & C14 cells.
Wow, we got our values. One thing you need to notice here is we don’t have any formula in the worksheet, but we just got the result of the “SUM” function in VBA.
#2 – Use VLOOKUP as a Worksheet Function
We will see how to use VLOOKUP in VBA. Assume below is the data you have in your excel sheet.
In the E2 cell, you had created a drop-down list of all the zones.
Based on the selection you made in the E2 cell, we need to fetch the Pin Code for the respective zone. But this time through VBA VLOOKUP, not worksheet VLOOKUP. Follow the below steps to apply VLOOKUP.
Step 1: Create a simple macro name in the Sub Procedure.
Sub Worksheet_Function_Example2() End Sub
Step 2: We need the result in the F2 cell. So start the code as Range (“F2”).Value =
Sub Worksheet_Function_Example2() Range ("F2").Value = End Sub
Step 3: To access the worksheet function, VLOOKUP starts the code as “WorksheetFunction.VLOOKUP.”
Sub Worksheet_Function_Example2() Range ("F2").Value = WorksheetFunction.Vlookup( End Sub
Step 4: One of the problems here is syntax will not give you any sort of guidance to work with VLOOKUP. You need to be absolutely sure about the syntax you are working on.
The first syntax of VLOOKUP is “Lookup Value.” In this case, our lookup value is E2 cell value, so write the code as Range (“E2”).Value
Sub Worksheet_Function_Example2() Range ("F2").Value = WorksheetFunction.Vlookup(Range ("E2").Value, End Sub
Step 5: Now, the second argument is our table array. In this case, our table array range is from A2 to B6. So the code will be Range (“A2:B6”)
Sub Worksheet_Function_Example2() Range ("F2").Value = WorksheetFunction.Vlookup(Range ("E2").Value,Range ("A2:B6"), End Sub
Step 6: The Third argument will be from which column we need the data from the table array. Here we need the data from the 2nd column, so the argument will be 2.
Sub Worksheet_Function_Example2() Range ("F2").Value = WorksheetFunction.Vlookup(Range ("E2").Value,Range ("A2:B6"),2, End Sub
Step 7: The final argument is range lookup, we need an exact match, so the argument is zero (0).
Sub Worksheet_Function_Example2() Range("F2").Value = WorksheetFunction.VLookup(Range("E2").Value, Range("A2:B6"), 2, 0) End Sub
So, we are done with the coding part. Now go to the worksheet and select any of the range.
Now go to your coding module and run the macro Using the F5 key or manually to get the pin code of the selected zone.
We cannot go back and run the macro every time, so let’s assign a macro to shapes. Insert one of the shapes in a worksheet.
Add a text value to the inserted shape.
Now right click and assign the macro name to this shape.
Click on ok after selecting the macro name.
Now, this shape holds the code of our VLOOKUP formula. So whenever you change the zone name, click on the button, it will update the values.
Things to Remember
- To access worksheet functions, we need to write the word “WorksheetFunction” or “Application.WorksheetFunction”
- We don’t have access to all the functions, only a few.
- We don’t see the actual syntax of worksheet functions, so we need to be absolutely sure of the function we are using.
This has been a guide to VBA Worksheet Function. Here we learn how to use Worksheet Functions like Sum and VLookup Function in excel VBA along with some simple to advanced examples. Below are some useful excel articles related to VBA –