Error Handling in VLOOKUP with VBA

sahaider

New Member
Joined
May 30, 2014
Messages
35
Hello ,
I have the following VBA code that includes VLOOKUP.

I am trying to include error trapping in the code so that when Vlookup returns #N/A write the code writes "Missing" in those cells.

VBA Code:
lastRow = Worksheets("LTE_Best Server by RSRP").Cells.SpecialCells(xlCellTypeLastCell).Row
With Worksheets("LTE_Best Server by RSRP")
For r1 = 2 To lastRow

.Range("G" & r1) = Application.WorksheetFunction.IfNa(VLookup(CStr(.Range("F", r1)), Worksheets("ALL 4G Hierarchy").Range("E2:J125000"), 5, False), "Missing")

Next

End With

But i am getting Sub/Function not defined for VLOOKUP when I run the code.

Can't figure out what is wrong in the code.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I think that you need a couple of changes. See how this goes

Rich (BB code):
.Range("G" & r1) = Application.WorksheetFunction.IfNa(WorksheetFunction.VLookup(CStr(.Range("F" & r1)), Worksheets("ALL 4G Hierarchy").Range("E2:J125000"), 5, False), "Missing")

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Maybe you can use something like this:
VBA Code:
Dim V As Variant
V = Application.VLookup(.........)
If Application.IsNA(V) Then
    Debug.Print "failure"
Else
    Debug.Print "success"
End If
 
Upvote 0
VBA Code:
.Range("G" & r1) = Application.WorksheetFunction.IfNa(Application.VLookup(CStr(.Range("F" & r1)), Worksheets("ALL 4G Hierarchy").Range("E2:J125000"), 5, False), "Missing")
 
Upvote 0
I think that you need a couple of changes. See how this goes

Rich (BB code):
.Range("G" & r1) = Application.WorksheetFunction.IfNa(WorksheetFunction.VLookup(CStr(.Range("F" & r1)), Worksheets("ALL 4G Hierarchy").Range("E2:J125000"), 5, False), "Missing")

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
hello , just tried as suggested, no success i'm afraid.
 
Upvote 0
How about
VBA Code:
Dim Vl As Variant
LastRow = Worksheets("LTE_Best Server by RSRP").Cells.SpecialCells(xlCellTypeLastCell).Row
With Worksheets("LTE_Best Server by RSRP")
   For r1 = 2 To LastRow
      
      Vl = Application.VLookup(CStr(.Range("F", r1)), Worksheets("ALL 4G Hierarchy").Range("E2:J125000"), 5, False)
      If IsError(Vl) Then
         .Range("G" & r1) = "Missing"
      Else
         .Range("G" & r1) = Vl
      End If
   Next

End With
 
Upvote 0
just tried as suggested, no success i'm afraid.
That gives us nothing to go on. ;)

In what way did it fail?
eg
An error message? (What was it?)
Wrong values returned?
Nothing?
Crashed excel?
etc


Surely that notation would error?
Rich (BB code):
Vl = Application.VLookup(CStr(.Range("F", r1)), Worksheets("ALL 4G Hierarchy").Range("E2:J125000"), 5, False)
 
Upvote 0
Surely that notation would error?
You're quite right it will, didn't notice that, it should be
VBA Code:
Dim Vl As Variant
LastRow = Worksheets("LTE_Best Server by RSRP").Cells.SpecialCells(xlCellTypeLastCell).Row
With Worksheets("LTE_Best Server by RSRP")
   For r1 = 2 To LastRow
      
      Vl = Application.VLookup(CStr(.Range("F" & r1)), Worksheets("ALL 4G Hierarchy").Range("E2:J125000"), 5, False)
      If IsError(Vl) Then
         .Range("G" & r1) = "Missing"
      Else
         .Range("G" & r1) = Vl
      End If
   Next

End With
 
Upvote 0
Solution

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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