Bypassing An Error associated with failed vlookup

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code:

Code:
With uf9_poststaff
            .cu2_start.Value = format(Application.WorksheetFunction.VLookup(CLng(.cu2_en.Value), rg_staff, 5, False), "hh:mm")
            .cu2_end.Value = format(Application.WorksheetFunction.VLookup(CLng(.cu2_en.Value), rg_staff, 6, False), "h:mm")
            .cu2_hours.Value = format(DateDiff("h", .cu2_end.Value, .cu2_start.Value) / 24, "standard")
            
            .cu3_start.Value = format(Application.WorksheetFunction.VLookup(CLng(.cu3_en.Value), rg_staff, 5, False), "hh:mm")
            .cu3_end.Value = format(Application.WorksheetFunction.VLookup(CLng(.cu3_en.Value), rg_staff, 6, False), "h:mm")
            .cu3_hours.Value = format(DateDiff("h", .cu3_end.Value, .cu3_start.Value) / 24, "standard")
            
            .wprk1_start.Value = format(Application.WorksheetFunction.VLookup(CLng(.wprk1_en.Value), rg_staff, 5, False), "hh:mm")
            .wprk1_end.Value = format(Application.WorksheetFunction.VLookup(CLng(.wprk1_en.Value), rg_staff, 6, False), "h:mm")
            .wprk1_hours.Value = format(DateDiff("h", .wprk1_end.Value, .wprk1_start.Value) / 24, "standard")
            
            .wbvd1_start.Value = format(Application.WorksheetFunction.VLookup(CLng(.wbvd1_en.Value), rg_staff, 5, False), "hh:mm")
            .wbvd1_end.Value = format(Application.WorksheetFunction.VLookup(CLng(.wbvd1_en.Value), rg_staff, 6, False), "h:mm")
            .wbvd1_hours.Value = format(DateDiff("h", .wbvd1_end.Value, .wbvd1_start.Value) / 24, "standard")
            
            .cu4_start.Value = format(Application.WorksheetFunction.VLookup(CLng(.cu4_en.Value), rg_staff, 5, False), "hh:mm")
            .cu4_end.Value = format(Application.WorksheetFunction.VLookup(CLng(.cu4_en.Value), rg_staff, 6, False), "h:mm")
            .cu4_hours.Value = format(DateDiff("h", .cu4_end.Value, .cu4_start.Value) / 24, "standard")
            
            .cu5_start.Value = format(Application.WorksheetFunction.VLookup(CLng(.cu5_en.Value), rg_staff, 5, False), "hh:mm")
            .cu5_end.Value = format(Application.WorksheetFunction.VLookup(CLng(.cu5_en.Value), rg_staff, 6, False), "h:mm")
            .cu5_hours.Value = format(DateDiff("h", .cu5_end.Value, .cu5_start.Value) / 24, "standard")
            
            .cu6_start.Value = format(Application.WorksheetFunction.VLookup(CLng(.cu6_en.Value), rg_staff, 5, False), "hh:mm")
            .cu6_end.Value = format(Application.WorksheetFunction.VLookup(CLng(.cu6_en.Value), rg_staff, 6, False), "h:mm")
            .cu6_hours.Value = format(DateDiff("h", .cu6_end.Value, .cu6_start.Value) / 24, "standard")
            
        End With

Any on e or more line may error when the vlookup fails to find the lookup value.
What kind of error checking code could I use to ignore the error associated with an inability to match and just simply move to the next line?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I adapted my code as an experiment ... as an example

Rich (BB code):
            .cu5_start.Value = format(Application.VLookup(CLng(.cu5_en.Value), rg_staff, 5, False), "hh:mm")
            If IsError(.cu5_start.Value) Then
                MsgBox "no match"
            Else
                .cu5_end.Value = format(Application.WorksheetFunction.VLookup(CLng(.cu5_en.Value), rg_staff, 6, False), "h:mm")
                .cu5_hours.Value = format(DateDiff("h", .cu5_end.Value, .cu5_start.Value) / 24, "standard")
            End If

But this leaves me with a "type mismatch" error with the line in red.
 
Upvote 0
Maybe:
Rich (BB code):
On Error Resume Next
.cu5_start.Value = Format(Application.VLookup(CLng(.cu5_en.Value), rg_staff, 5, False), "hh:mm")
            If IsError(.cu5_start.Value) Then
                MsgBox "no match"
                On Error GoTo 0
            Else
                .cu5_end.Value = Format(Application.WorksheetFunction.VLookup(CLng(.cu5_en.Value), rg_staff, 6, False), "h:mm")
                .cu5_hours.Value = Format(DateDiff("h", .cu5_end.Value, .cu5_start.Value) / 24, "standard")
            End If
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,578
Members
449,174
Latest member
chandan4057

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