VBA Worksheet Function

Updated on January 2, 2024
Article byJeevan A Y
Edited byAshish Kumar Srivastav
Reviewed byDheeraj Vaidya, CFA, FRM

Excel VBA Worksheet Functions

Worksheet function in VBA one may use 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. Still, if we want to execute the code in the specific worksheet, we use the Worksheet function. This function has various uses and applications in VBA.

The best thing about VBA is how we use formulas in worksheets. Similarly, VBA too has its functions. However, if this is the best, it also has a beautiful thing. That is, “we can also use Worksheet functions in VBA.”

You heard it right. We can access Worksheet functions in VBA also. We can access some of the worksheet functions while writing and making them part of our code.

VBA Worksheet Function

You are free to use this image on your website, templates, etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA Worksheet Function (wallstreetmojo.com)

How to use Worksheet Functions in VBA?

You can download this VBA WorksheetFunction Template here – VBA WorksheetFunction Template

In the worksheet, all the formulas start with equal (=) sign, similarly, in VBA coding, to access worksheet formulas, we should use the word “WorksheetFunction.

Before entering any worksheet formula, you need to mention the “WorksheetFunction” object name, put a dot (.), and get a list of all the available functions under this object.

WorksheetFunction

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.read more, which will add more value to your coding knowledge.

–>> If you want to learn Excel and VBA professionally, then ​Excel VBA All in One Courses Bundle​ (35+ hours) is the perfect solution. Whether you’re a beginner or an experienced user, this bundle covers it all – from Basic Excel to Advanced Excel, Macros, Power Query, and VBA.

#1 – Simple SUM Worksheet Functions

To start with Worksheet functions, apply the simple SUM function in excelSUM Function In ExcelThe SUM function in excel adds the numerical values in a range of cells. Being categorized under the Math and Trigonometry function, it is entered by typing “=SUM” followed by the values to be summed. The values supplied to the function can be numbers, cell references or ranges.read more to add numbers from the worksheet.

Assume you have monthly sales and cost data in the worksheet like the below one.

WorksheetFunction Example 1

In B14 and C14, we need to arrive at the total of the above numbers. Follow the steps below to start applying the SUM function in Excel VBA.

Step 1: Create a simple, excel macroExcel MacroA macro in excel is a series of instructions in the form of code that helps automate manual tasks, thereby saving time. Excel executes those instructions in a step-by-step manner on the given data. For example, it can be used to automate repetitive tasks such as summation, cell formatting, information copying, etc. thereby rapidly replacing repetitious operations with a few clicks. read more name.

Code:

Sub Worksheet_Function_Example1()

End Sub
WorksheetFunction Example 1-1

Step 2: Since we need the result in cell B14, start the code as Range (“B14”).Value =

Code:

Sub Worksheet_Function_Example1()

  Range("B14").Value =

End Sub
WorksheetFunction Example 1-2

Step 3: In B14, we need the value as the result of the sum of the numbers. So to access the SUM function from the worksheet, start the code as “WorksheetFunction.

Code:

Sub Worksheet_Function_Example1()

Range("B14").Value = WorksheetFunction.

End Sub
WorksheetFunction Example 1-3

Step 4: The moment you put a dot (.), it will start to display the functions available. So, select SUM from this.

Code:

Sub Worksheet_Function_Example1()

   Range("B14").Value = WorksheetFunction.Sum

End Sub
WorksheetFunction Example 1-4

Step 5: Reference the above numbers: Range (“B2:B13”).

Code:

Sub Worksheet_Function_Example1()

  Range("B14").Value = WorksheetFunction.Sum(Range("B2:B13"))

End Sub
WorksheetFunction Example 1-5

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.read more.

Code:

Sub Worksheet_Function_Example1()

  Range("B14").Value = WorksheetFunction.Sum(Range("B2:B13"))
  Range("C14").Value = WorksheetFunction.Sum(Range("C2:C13"))

End Sub
WorksheetFunction Example 1-6

Step 7: Run this code manually or use the F5 key to have a total in B14 and C14 cells.

WorksheetFunction Example 1-7

We got our values. You need to notice that 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 VBAHow To Use VLOOKUP In VBAThe functionality of VLOOKUP in VBA is similar to that of VLOOKUP in a worksheet, and the method of using VLOOKUP in VBA is through an application. Method WorksheetFunctionread more. Assume below is the data you have in your Excel sheet.

WorksheetFunction Example 2

In the E2 cell, you had created a drop-down list of all the zones.

WorksheetFunction Example 2-1

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. So, follow the below steps to apply VLOOKUPApply VLOOKUPThe VLOOKUP excel function searches for a particular value and returns a corresponding match based on a unique identifier. A unique identifier is uniquely associated with all the records of the database. For instance, employee ID, student roll number, customer contact number, seller email address, etc., are unique identifiers. read more.

Step 1: Create a simple Macro name in the sub procedure.

Code:

Sub Worksheet_Function_Example2()

End Sub
WorksheetFunction Example 2-2

Step 2: We need the result in the F2 cell. So start the code as Range (“F2”).Value =

Code:

Sub Worksheet_Function_Example2()

  Range ("F2").Value =

End Sub
WorksheetFunction Example 2-3

Step 3: To access the worksheet function, VLOOKUP starts the code as “WorksheetFunction.VLOOKUP.”

Code:

Sub Worksheet_Function_Example2()

  Range ("F2").Value = WorksheetFunction.Vlookup(

End Sub
WorksheetFunction Example 2-4

Step 4: One of the problems here is syntax will not guide you to work with VLOOKUP. It would help if you knew the syntax you were working on.

The first syntax of VLOOKUP is “Lookup Value.” In this case, our lookup value is the E2 cell, so write the code as  Range (“E2”).Value

Code:

Sub Worksheet_Function_Example2()

  Range ("F2").Value = WorksheetFunction.Vlookup(Range ("E2").Value,

End Sub
Worksheet Function Example 2-5

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”).

Code:

Sub Worksheet_Function_Example2()

  Range ("F2").Value = WorksheetFunction.Vlookup(Range ("E2").Value,Range ("A2:B6"),

End Sub
Worksheet Function Example 2-6

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 second column so that the argument will be 2.

Code:

Sub Worksheet_Function_Example2()

  Range ("F2").Value = WorksheetFunction.Vlookup(Range ("E2").Value,Range ("A2:B6"),2,

End Sub
Worksheet Function Example 2-7

Step 7: The final argument is range lookup, we need an exact match, so the argument is zero (0).

Code:

Sub Worksheet_Function_Example2()

  Range("F2").Value = WorksheetFunction.VLookup(Range("E2").Value, Range("A2:B6"), 2, 0)

End Sub
Worksheet Function Example 2-8

So, we have completed the coding part. Now, go to the Worksheet and select any of the ranges.

Worksheet Function Example 2-9

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.

Worksheet Function Example 2-10

We cannot go back and run the Macro every time, so let’s assign a Macro to shapes. Then, insert one of the shapes into a worksheet.

Worksheet Function Example 2-11

Add a text value to the inserted shape.

WorksheetFunction Example 2-12

Now, right-click and assign the Macro name to this shape.

Worksheet Function Example 2-13

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 to update the values.

Worksheet Function Example 2-14

Things to Remember

  • To access worksheet functions, we need to write the word “WorksheetFunction” or “Application.WorksheetFunction”
  • We do not have access to all the functions, only a few.
  • We do not see the actual syntax of Worksheet functions, so we must be sure of the function we are using.

This article is a guide to VBA Worksheet Function. Here, we learn how to use Worksheet functions like SUM and VLOOKUP in Excel VBA and some simple to advanced examples. Below are some useful Excel articles related to VBA: –