VBA Vlookup multiple sheets

Evie76

New Member
Joined
Jan 17, 2022
Messages
36
Office Version
  1. 365
Platform
  1. Windows
Hi

Below I have a code which is great to do vlookup via vba on just one sheet. However i need it to look at 3 sheets in the same workbook. Can anyone help.

VBA Code:
Sub Vlookup()

Set myrange = Sheet1.Range("B:E")

Set MeasureReference = Range("c3")

Set Status = Range("D3")

Status.Value = Application.WorksheetFunction.Vlookup(MeasureReference, myrange, 2, False)

Set ManagingAgent = Range("e3")

ManagingAgent.Value = Application.WorksheetFunction.Vlookup(MeasureReference, myrange, 3, False)

Set MARefNo = Range("f3")

MARefNo.Value = Application.WorksheetFunction.Vlookup(MeasureReference, myrange, 4, False)

End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi Evie76,

Maybe this:

VBA Code:
Option Explicit
Sub Vlookup()

    Dim myrange As Range
    Dim MeasureReference As Range
    Dim Status As Range
    Dim ManagingAgent As Range
    Dim MARefNo As Range
    Dim varSheet As Variant
    Dim wsSheet As Worksheet
    
    Application.ScreenUpdating = False

    Set myrange = Sheet1.Range("B:E") '<-Assumed this code name reference is static
    
    For Each varSheet In Array("Sheet1", "Sheet2", "Sheet3") '<-Actual tab names for the VLOOKUP - not their code name as above
        On Error Resume Next
           Set wsSheet = ThisWorkbook.Sheets(CStr(varSheet))
            If Err.Number = 0 Then
                Set MeasureReference = wsSheet.Range("C3")
                Set Status = wsSheet.Range("D3")
                Status.Value = Application.WorksheetFunction.Vlookup(MeasureReference, myrange, 2, False)
                Set ManagingAgent = wsSheet.Range("E3")
                ManagingAgent.Value = Application.WorksheetFunction.Vlookup(MeasureReference, myrange, 3, False)
                Set MARefNo = wsSheet.Range("F3")
                MARefNo.Value = Application.WorksheetFunction.Vlookup(MeasureReference, myrange, 4, False)
            Else
                MsgBox "there is no tab called """ & CStr(varSheet) & """ in the workbook.", vbExclamation
            End If
        On Error GoTo 0
    Next varSheet
        
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Solution

Forum statistics

Threads
1,214,965
Messages
6,122,499
Members
449,089
Latest member
Raviguru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top