VBA Worksheet Function

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.

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, 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.

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.

#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.

WorksheetFunction Example 1

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.

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 in order 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: Now give the reference of the above numbers, i.e., 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: Now run this code manually or using the F5 key to have a total in B14 & C14 cells.

WorksheetFunction Example 1-7

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.

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. Follow the below steps to apply VLOOKUP.

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 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

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 2nd column, so 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 are done with the coding part. Now go to the worksheet and select any of the range.

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. Insert one of the shapes in 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, it will 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 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.

Recommended Articles

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 examplesBelow are some useful excel articles related to VBA –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>