Master Excel VBA VLOOKUP in Another Worksheet with Practical Example

Bobby Jul 13, 2026

Ever found yourself needing to pull data from one Excel worksheet to another, wishing for a simple way to do so? Enter the VLOOKUP function and its powerful partner, VBA (Visual Basic for Applications). This duo can enhance your Excel prowess by streamlining your data management. Today, we're going to explore how to use VBA to enhance your VLOOKUP capabilities within another worksheet.

Excel VBA Copy Data from Multiple Workbooks
Excel VBA Copy Data from Multiple Workbooks

Before we dive into the VBA realm, let's quickly refresh our memories about what VLOOKUP actually does. VLOOKUP retrieves data from a table or range and places it in a specified location. But what if you want to define the range dynamically or automate the process? That's where VBA steps in.

Use Amazing Hacks in Excel with VLOOKUP Function
Use Amazing Hacks in Excel with VLOOKUP Function

Understanding the VLOOKUP Function

VLOOKUP takes four arguments: lookup_value, table_array, col_index_num, and optional [range_lookup]. In simpler terms, it's searching for your specified value within a table, and if found, it returns the value in the column you specify.

VBA UserForm
VBA UserForm

Let's say you have two worksheets, 'Sheet1' with your main data, and 'Sheet2' where you want to pull data into. Your task is to pull data from 'Sheet1' into 'Sheet2' using a common identifier, like 'ID'.

Defining Your Range Dynamically

Master Excel VBA with Professional Tips and Best Practices
Master Excel VBA with Professional Tips and Best Practices

VBA allows you to define your range dynamically based on criteria, which is essential if your data isn'tstatic. Using the Range.Find method, you can search for a specific value and then adjust your range accordingly.

Let's create a VBA code that does this. Open the Visual Basic Editor (VBE) in Excel by pressing Alt + F11, then insert a new module and try this:

```vba Sub VLOOKUP_Dynamic() Dim ws1 As Worksheet Dim ws2 As Worksheet Dim lastRow As Long Dim i As Long 'Set references to your worksheets Set ws1 = ThisWorkbook.Sheets("Sheet1") Set ws2 = ThisWorkbook.Sheets("Sheet2") 'Find the last row with data in the ID column on ws1 lastRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row 'Loop through each row, searching for the ID number on ws2 For i = 2 To lastRow 'Use VLOOKUP to retrieve data ws2.Cells(i, 2).Value = WorksheetFunction.VLOOKUP(ws1.Cells(i, "A").Value, ws1.Range("A1:B" & lastRow), 2, False) Next i End Sub ```

In this code, we first establish references to 'Sheet1' and 'Sheet2'. We then find the last row with data in the ID column on 'Sheet1'. We loop through each row, finding the ID number on 'Sheet2', and use VLOOKUP to retrieve the corresponding data.

How to Do a VLOOKUP in an Excel Spreadsheet
How to Do a VLOOKUP in an Excel Spreadsheet

Pulling Data Automatically

VBA can automate tasks, allowing your VLOOKUP to run on demand or even at specific intervals. Let's modify our previous code to run when a button is clicked:

```vba Sub VLOOKUP_Automatic() Dim ws1 As Worksheet Dim ws2 As Worksheet Dim lastRow As Long Dim i As Long 'Set references to your worksheets Set ws1 = ThisWorkbook.Sheets("Sheet1") Set ws2 = ThisWorkbook.Sheets("Sheet2") 'Find the last row with data in the ID column on ws1 lastRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row 'Loop through each row For i = 2 To lastRow 'Use VLOOKUP to retrieve data ws2.Cells(i, 2).Value = WorksheetFunction.VLOOKUP(ws1.Cells(i, "A").Value, ws1.Range("A1:B" & lastRow), 2, False) Next i 'Show a message box to confirm completion MsgBox "Data populated successfully!" End Sub ```

In this modified code, a message box will popup once the operation is done, confirming the successful population of data.

How to VLOOKUP in 30 seconds
How to VLOOKUP in 30 seconds

Remember, the beauty of VBA lies in its flexibility and automation capabilities. When combined with powerful functions like VLOOKUP, the possibilities for data manipulation are endless. So, go forth and conquer your data management challenges!

VBA for Excel Cheat Sheet
VBA for Excel Cheat Sheet
an image of a spreadsheet with the text and numbers added
an image of a spreadsheet with the text and numbers added
Vlookup
Vlookup
Excel Vlookup Examples With Multiple Sheets - freeexcellearn
Excel Vlookup Examples With Multiple Sheets - freeexcellearn
the vlookup from another sheet is shown in this screenshoto screen
the vlookup from another sheet is shown in this screenshoto screen
VBA to Create PDF from Excel Sheet & Email It With Outlook
VBA to Create PDF from Excel Sheet & Email It With Outlook
Free Excel VBA Book
Free Excel VBA Book
How to use Excel VLOOKUP
How to use Excel VLOOKUP
Excel VBA UserForm with Navigation Buttons 🚀 | See it in Action! #shorts
Excel VBA UserForm with Navigation Buttons 🚀 | See it in Action! #shorts