Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

VBA VLOOKUP Function

VBA VLOOKUP Function (Table of Contents)

  • VBA VLOOKUP Function
  • How to Use VBA VLOOKUP in Excel?

VBA VLOOKUP Function

VBA has many built-in functions to deal with. Vlookup is a worksheet function which can be used in VBA too. If you are aware of normal VLOOKUP function it will be much easier for you to understand VBA function.

Normal VLOOKUP has 4 mandatory arguments to deal with. In VBA function too it is the same as the worksheet function.

Formula of VLOOKUP Function

The Formula includes 4 mandatory arguments.

  • Lookup Value: This is the base values you are searching. Based on this we look for a value in the table.
  • Table Value: This is the table which holds all the values. From this table using lookup value, we will fetch the data.
  • Column Index Number: This is the column number from the table we are looking for. In a table, there are many columns, out of many columns we only require the column which we are looking for the data.
  • Range Lookup: Here we need to specify what kind of result we want. If we want the exact match we need to mention as FALSE or 0, if we want the approximate match we can mention as TRUE or 1.

Where to Write Macros?

VBA is accommodated under Developer tab in excel. If you are not able to find the developer tab follow the below steps to unhide this.

Step 1: Click on File.

Step 2: Under File, click on Options.

Step 3: Select Customize Ribbon.

Step 4: On the right-hand side make sure the checkbox of the Developer tab is ticked.

Now you must see the Developer tab in the Ribbon.

How to use VBA VLOOKUP Function in Excel?

VBA VLOOKUP Function is very simple to use. Let us now see how to use Vba Vlookup Function in Excel with the help of some examples.

Example #1

I have a sales table for different products. It has product name and sales made against that product.

In cell E2, I want to know the sales value of TV using VBA VLOOKUP function.

Step 1: Go to the Developer tab and click on Visual Basic

Step 2: Once you click on Visual Basic it will open the below window for you

Step 3: Click on Insert and select Module

Step 4: Once you click on Module it will insert the new module for you. Double click on that newly inserted module to write your VLOOKUP code.

Step 5: Copy and paste the below code to your newly inserted module.

Sub Vlookup_Example()
Dim rng As Range, FinalResult As Variant
Set rng = Sheets("Example 1").Range("E2")
FinalResult = Application.WorksheetFunction.VLookup(Range("D2"), Range("A2:B7"), 2, False)
rng = FinalResult
End Sub

Let me break down the code to explain it to you.

Line 1: Dim rng as Range, FinalResult as Variant

This is the first line of the code. Using DIM statement I have declared two variables one is rng & another one is FinalResult.

Rng holds Range as the variable and FinalResult holds Variant as the variable.

Line 2: Set rng = Sheets(“Example 1”).Range(“E2”)

Previous declared variable rng is an object variable so we need to set to which range actually we are referring to. I have set the range to E2 cell. This is actually where I need the result to be displayed.

Line 3: FinalResult = Application.WorksheetFunction.VLookup(Range(“D2”), Range(“A2:B7”), 2, False)

This is the important line of the code. FinalResult holds the variable of the variant i.e. any kind of data type. FinalResugetsget the data from the function VLOOKUP.

Line 4: rng = FinalResult

Initial variable rng is equal to the value of FinalResult. Rng means E2 cell, FinalResult means value returned by the VLOOKUP function.

Step 6: Hit the RUN button or Press F5 to execute the code

Step 7: We will get the below output.

Example #2

Take the same data from the above example. Here I am going to create names and apply the same in the VLOOKUP. The code will the same as the previous example. Only in VLOOKUP, I am changing references.

Sub Vlookup_Example1()
Dim rng As Range, FinalResult As Variant, Table_Range As Range, LookupValue As Range
Set rng = Sheets("Example 1").Range("E2")
Set Table_Range = Sheets("Example 1").Range("A2:B7")
Set LookupValue = Sheets("Example 1").Range("D2")
FinalResult = Application.WorksheetFunction.VLookup(LookupValue, Table_Range, 2, False)
rng = FinalResult
End Sub

Lookup value is set to D2 cell in the name of LookupValue. Table Array is set to A2 to B7 in the name of Table_Range.

Create a Macro Button

Once the macro code is written we need to design the macro.

Step 1: Go to Insert and Select the rectangular shape.

Step 2: Draw a rectangular shape

Step 3: Once the Rectangular shape has drawn right click on the rectangular shape and click on Assign Macro.

Step 4: Once you click on Assign Macro it will open up all the macro names. Select the macro name you wish to assign. I am assigning Example 1 macro name to the rectangular shape.

Step 5: Now rectangular shape has become a macro button. Once you click on the rectangular shape, you will get the below output.

You can change the lookup value cell D2 and get the result. In the below image, I have changed the D2 cell value to Mobile.

Things to Remember About VBA VLOOKUP

  • In VBA too VLOOKUP works the same as the worksheet function.
  • Declaring variables and assigning data type is the important thing here.
  • In the resulting cell, we do not see any kind of formula. Any value returned by VBA function does not hold any kind of formula.

You can download this VBA VLOOKUP Excel Template here – VBA VLOOKUP Excel Template

Recommended Articles

This has been a guide to Excel VBA VLOOKUP function. Here we discuss the VLOOKUP Formula and how to use VBA VLOOKUP in Excel along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. How to Use Combo Box in Excel?
  2. Create Bubble Chart in MS Excel
  3. How to Create HYPERLINK in Excel?
  4. Guide to Excel Stacked Column Chart

The post VBA VLOOKUP Function appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Share the post

VBA VLOOKUP Function

×

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×