Vba vlookup to return back not #NA

Boboka

New Member
Joined
Jan 19, 2022
Messages
15
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
ws1.Range("K4", "K" & LastRow).Value _ 
= WorksheetFunction.VLookup(ws1.Range("D4", "D" & LastRow), ws2.Range("C2:C182"), 1, 0)

Hi, the above vlookup code works. But what code should I add so that if the code return #NA it show a blank cell.

For the same code, ws2.Range("C2:C182"), how can I make it dynamic? Now it is hard coded. Meaning that whenever I add a new row in worksheets (ws2) it will auto update the rows in the formula. So that I don’t have to go into code and update C2:C1xx every time when there is a new row.

Thanks in advance. The full code is below
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim LastRow As Long
Dim LastRow1 As Long

Set wb = ThisWorkbook
Set ws1 = wb.Sheets("CHECKS")
Set ws2 = wb.Sheets("LEADERS")

LastRow = ws1.Cells(Rows.Count, 1).End(xlUp).Row
LastRow1 = ws2.Cells(Rows.Count, 1).End(xlUp).Row

ws1.Range("K4", "K" & LastRow).Value _
= WorksheetFunction.VLookup(ws1.Range("D4", "D" & LastRow), ws2.Range("C2:C182"), 1, 0)

ws1.Range("L4", "L" & LastRow).Value _
= WorksheetFunction.VLookup(ws1.Range("D4", "D" & LastRow), ws2.Range("F2:F182"), 1, 0)



End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi
Try
VBA Code:
ws1.Range("K4", "K" & LastRow).Value _
= Application.IfNa(WorksheetFunction.VLookup(ws1.Range("D4", "D" & LastRow), ws2.Range("C2:C" & LastRow1), 1, 0), "")
 
Upvote 0
Or
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim LastRow As Long
Dim LastRow1 As Long

Set wb = ThisWorkbook
Set ws1 = wb.Sheets("CHECKS")
Set ws2 = wb.Sheets("LEADERS")

LastRow = ws1.Cells(Rows.Count, 1).End(xlUp).Row
LastRow1 = ws2.Cells(Rows.Count, 1).End(xlUp).Row

ws1.Range("K4", "K" & LastRow) _
= Application.IfNa(WorksheetFunction.VLookup(ws1.Range("D4", "D" & LastRow), ws2.Range("C2:C" & LastRow1), 1, 0), "")

ws1.Range("L4", "L" & LastRow).Value _
= Application.IfNa(WorksheetFunction.VLookup(ws1.Range("D4", "D" & LastRow), ws2.Range("F2:F" & LastRow1), 1, 0), "")
 
Upvote 0
Thanks, it work for the first vlookup. But I realise that the second formula last part
VBA Code:
ws2.Range(“F2:F” & LastRow1), 1, 0), “ “)
need to change F2:F to a look up range/array.

VBA Code:
ws1.Range("L4", "L" & LastRow).Value _ 
= Application.IfNa(WorksheetFunction.VLookup(ws1.Range("D4", "D" & LastRow), ws2.Range("C2:F182" & LastRow1), 4, 0), "")
this work but how to make C2:F182 dynamic?

Thanks in advance.
 
Upvote 0
Try:

ws1.Range("L4", "L" & LastRow).Value _
= Application.IfNa(WorksheetFunction.VLookup(ws1.Range("D4", "D" & LastRow), ws2.Range("C2:F" & LastRow1), 4, 0), "")
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,022
Members
448,939
Latest member
Leon Leenders

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