radonwilson
Board Regular
- Joined
- Jun 23, 2021
- Messages
- 50
- Office Version
- 2019
- Platform
- Windows
How to handle the error of 2 looping vlookups which are using the same counter/offsetting variable? I have successfully handled errors for vlookup 1 until my second vlookup1 started running. As soon as my vlookup 2 falls into error it immediately goes to the error label which in results affecting my vlookup 1 , how can I handle errors of both these vlookups separately?
Here is my code, please have a look. I don't want to use On Error Resume Next.
Here is my code, please have a look. I don't want to use On Error Resume Next.
test.xlsm | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | |||
1 | Customer Name | Unit Price | |||||||||||
2 | Allen Rosenblatt | 15.74 | VLOOKUP 1 | VLOOKUP 2 | |||||||||
3 | Barry French | 216.85 | Customer Name | Unit Price | Customer Name | Unit Price | |||||||
4 | Carl Ludwig | 20.88 | Carl Ludwig | 20.88 | Edward Hooks | 500.98 | |||||||
5 | Carlos Soltero | 8.4 | Carlos Soltero | Not Found | Eugene Barchas 1 | ||||||||
6 | Claudia Miner | 5.28 | Claudia Miner 1 | Not Found | Jack Garza | 40.96 | |||||||
7 | Clay Rozendal | 195.99 | Neola Schneider 1 | Not Found | Jim Radford | 221.98 | |||||||
8 | Don Miller | 2.88 | Sylvia Foulston | Not Found | Julia West 1 | ||||||||
9 | Edward Hooks | 500.98 | |||||||||||
10 | Eugene Barchas | 625.85 | |||||||||||
11 | Jack Garza | 40.96 | |||||||||||
12 | Jim Radford | 221.98 | |||||||||||
13 | Julia West | 95.95 | |||||||||||
14 | Muhammed MacIntyre | 38.94 | |||||||||||
15 | Neola Schneider | 39.89 | |||||||||||
16 | Sylvia Foulston | 201.96 | |||||||||||
Sheet1 |
VBA Code:
Option Explicit
Sub vlookup()
Dim c As Integer
On Error GoTo Last
For c = 1 To 5
Range("G" & c + 3).Value = Application.WorksheetFunction.vlookup( _
Range("f" & c + 3).Value, Range("a:b"), 2, 0)
Next c
For c = 1 To 5
Range("k" & c + 3).Value = Application.WorksheetFunction.vlookup( _
Range("j" & c + 3).Value, Range("a:b"), 2, 0)
Next c
Exit Sub
Last:
If Err.Number = 1004 Then
Range("G" & c + 3).Value = "Not Found"
Resume Next
End If
End Sub