How to use target.row in vba excel with vlookup formulas

Leorand

New Member
Joined
Nov 25, 2022
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hello Guys, I got some question about the tittle above.
I want to make VBA program to match data using IFERROR(VLOOKUP) formulas, and the code like this :

VBA Code:
Set TableRange = Range("D4:D10000")
result = Application.IfError(Application.VLookup(TableRange, Sheet5.Range("A2:C170"), 3, False), "")

If Range("E" & Target.Row).Value = "" Then
    Range("E" & Target.Row).Value = result
End If

In this code I use Target.Row function to make whenever the D row has data, E row will show the match data using Vlookup formulas.
For the first Input in row D4, row E4 showing the match data, but when it goes to row D5 with different data, row E5 still showing the same data as row E4.
Like this :
1681090577205.png

So how do I solve this case? Is there are another method?
I need your help guys.
It will help me a lot.

Best Regards
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I don't think you can use a range as lookup value. One good practice could be:
VBA Code:
Set TableRange = Range("D4:D10000")
If Not Intersect(Target, TableRange) Is Nothing Then
  result = Application.IfError(Application.VLookup(Target.Value, Sheet5.Range("A2:C170"), 3, False), "")
End If
If Range("E" & Target.Row).Value = "" Then
    Range("E" & Target.Row).Value = result
End If
 
Upvote 1
Solution
I don't think you can use a range as lookup value. One good practice could be:
VBA Code:
Set TableRange = Range("D4:D10000")
If Not Intersect(Target, TableRange) Is Nothing Then
  result = Application.IfError(Application.VLookup(Target.Value, Sheet5.Range("A2:C170"), 3, False), "")
End If
If Range("E" & Target.Row).Value = "" Then
    Range("E" & Target.Row).Value = result
End If
Thank you for your help Flashbond.
The code work perfectly!!
 
Upvote 0
Glad it did help! Thanks for the feedback (y)

EDIT: It would be wise to move If block inside the Intersect.
VBA Code:
Set TableRange = Range("D4:D10000")
If Not Intersect(Target, TableRange) Is Nothing Then
  result = Application.IfError(Application.VLookup(Target.Value, Sheet5.Range("A2:C170"), 3, False), "")
  If Range("E" & Target.Row).Value = "" Then
      Range("E" & Target.Row).Value = result
  End If
End If
 
Last edited by a moderator:
Upvote 0
Glad it did help! Thanks for the feedback (y)

EDIT: It would be wise to move If block inside the Intersect.
VBA Code:
Set TableRange = Range("D4:D10000")
If Not Intersect(Target, TableRange) Is Nothing Then
  result = Application.IfError(Application.VLookup(Target.Value, Sheet5.Range("A2:C170"), 3, False), "")
  If Range("E" & Target.Row).Value = "" Then
      Range("E" & Target.Row).Value = result
  End If
End If
Yeah, I just change the
VBA Code:
TableRange
into
VBA Code:
Target.Value
, for the rest code is work's well.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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