Error Handling with failed vlookup

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,527
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I'm looking for some direction with enhancing this code:

Code:
       Case "L18"
            Dim dfcust As String
            Dim wshgrp As Worksheet
            Set wshmain = Worksheets("Main")
            Set wshgrp = Worksheets("Group_Data")
                 
            rnum = Application.Match(Worksheets("varhold").Range("A17"), Worksheets("CONTROL_1").Range("A:A"), 0)
                 
            With Worksheets("CONTROL_1")
                    .Range("F" & rnum) = wshmain.Range("L18")
            End With
                  
            With wshmain
                .Unprotect
                With .Range("Y18:AF18")
                    '.Locked = False
                    .Interior.Color = RGB(112, 156, 148)
                End With
                dfcust = Left(wshmain.Range("L18"), 3)
                .Range("Y18") = Application.VLookup(dfcust & "*", wshgrp.Range("I:J"), 1, False)
                .Protect
            End With

With this code, wshmain.range("Y18") is populated with the value associated with the vlookup. However, problems exist when the vlookup fails. If the vlookup fails, I don't want to try to populate wshmain.range, just simply .protect and abandon.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,367
Office Version
  1. 365
Platform
  1. Windows
Use a variable for the result of the VLookup just as you do earlier in the code for Match.

Then check to see if it's failed using IsError.
Code:
Dim Ans As Variant

' other code

Ans =  Application.VLookup(dfcust & "*", wshgrp.Range("I:J"), 1, False)

If Not IsError(Ans) Then
    .Range("Y18").Value = Ans
End If
PS Why are you using VLookup anyway?
 
Upvote 0

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Try changing

.Range("Y18") = Application.VLookup(dfcust & "*", wshgrp.Range("I:J"), 1, False)

to

x = Application.VLookup(dfcust & "*", wshgrp.Range("I:J"), 1, False)
If Not IsError(x) Then .Range("Y18") = x
 
Upvote 0

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,527
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Thanks Folks ...
That seems to have done the trick.
 
Upvote 0

Forum statistics

Threads
1,195,632
Messages
6,010,810
Members
441,569
Latest member
PeggyLee

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
Top