VBA vlookup from another sheet?

bkuu

New Member
Joined
Jun 14, 2021
Messages
18
Office Version
  1. 2019
Platform
  1. Windows
I have this code working at the moment

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Me.Range("D2:D500")) Is Nothing Then
Application.EnableEvents = False 'Disables events to prevent endless loop

On Error GoTo Finalize 'Re-enable events

Target.Value = Application.VLookup(Target.Value, Me.Range("Q:R"), 2, False)
End If

Finalize:
Application.EnableEvents = True
End Sub

However, I'd like to change the vlookup source ("Q:R") to same the column but on another sheet ("Sheet2"). I have been trying to input Worksheet("Sheet2") any where in the code but I don't think I'm doing it correctly. Any ideas?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("D2:D500")) Is Nothing Then
Application.EnableEvents = False 'Disables events to prevent endless loop

On Error GoTo Finalize 'Re-enable events

Dim rng as Range
Set rng = Sheets("Sheet2").Range("Q:R")
Target.Value = Application.VLookup(Target.Value, rng, 2, False)
End If

Finalize:
Application.EnableEvents = True
End Sub
BTW, you can get rid of the "Me.". It serves no purpose here.
 
Upvote 0
Solution
Try...

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Me.Range("D2:D500")) Is Nothing Then
        Application.EnableEvents = False 'Disables events to prevent endless loop
        On Error GoTo Finalize 'Re-enable events
        Target.Value = Application.VLookup(Target.Value, ThisWorkbook.Sheets("Sheet2").Range("Q:R"), 2, False)
    End If
 
Finalize:
    Application.EnableEvents = True
End Sub
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("D2:D500")) Is Nothing Then
Application.EnableEvents = False 'Disables events to prevent endless loop

On Error GoTo Finalize 'Re-enable events

Dim rng as Range
Set rng = Sheets("Sheet2").Range("Q:R")
Target.Value = Application.VLookup(Target.Value, rng, 2, False)
End If

Finalize:
Application.EnableEvents = True
End Sub
BTW, you can get rid of the "Me.". It serves no purpose here.
works! Thank you
 
Upvote 0
Try...

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Me.Range("D2:D500")) Is Nothing Then
        Application.EnableEvents = False 'Disables events to prevent endless loop
        On Error GoTo Finalize 'Re-enable events
        Target.Value = Application.VLookup(Target.Value, ThisWorkbook.Sheets("Sheet2").Range("Q:R"), 2, False)
    End If
 
Finalize:
    Application.EnableEvents = True
End Sub
thanks for the reply. Your method works as well. Looks like I almost had it , just got the Workbooksheet part wrong
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,223,856
Messages
6,175,029
Members
452,606
Latest member
jkondrat14

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