VBA: VLOOKUP If Certain Criteria Is Met

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hello Gurus,

I'm currently have below code

DATA 1 Sheet:

ITEM 1ITEM 2ITEM 3ITEM 4DATA 4
001
002
003

Note: In cell E1, that is the Sheet where the data should be look up. Usually, manually filled as there are too many data sheet tabs.

In the code below, it will unhide the DATA 4 sheet (based on E1 cell value in Data 1 Sheet) and should lookup the data.

DATA 4 Sheet look like this:

ITEM 1ITEM 2ITEM 3ITEM 4NOTE
001LOS ANGELES1000SHAMPOO
002BALTIMORE500DETERGENT
003NEW YORK100MISC


VBA Code:
Sub VlookUp()

    Dim lookupSheet As Worksheet
    Dim targetSheet As Worksheet
    Dim lookupValue As Range
    Dim lastRow As Long
   
    Set lookupSheet = ThisWorkbook.Sheets("DATA 1")
    Set lookupValue = lookupSheet.Range("A2")
   
    Set targetSheet = ThisWorkbook.Sheets(lookupSheet.Range("E1").Value)
    targetSheet.Visible = xlSheetVisible

    lookupSheet.Range("B2").Formula = "=VLOOKUP(A:A, '" & lookupValue.Value & "'!A:E, 2, 0)"
    lookupSheet.Range("C2").Formula = "=VLOOKUP(A:A, '" & lookupValue.Value & "'!A:E, 3, 0)"
    lookupSheet.Range("D2").Formula = "=VLOOKUP(A:A, '" & lookupValue.Value & "'!A:E, 4, 0)"
 
    ' Copy formulas to last non-blank cells
    lastRow = lookupSheet.Cells(Rows.Count, "A").End(xlUp).Row
    lookupSheet.Range("B2:E2").AutoFill Destination:=lookupSheet.Range("B2:E" & lastRow), Type:=xlFillDefault


End Sub

= = = = =

When I run the macro, it is asking to open the file so I believe there is something to correct in my code.

Appreciate the help.
 
Last edited by a moderator:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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