I have a VBA code the performs a Vlookup. The code works well, but I want to make a change.
Currently, when a match is not found, the code goes to an error handler. However, I want to perform another check before I go to the error handler. I want the code to re-run the Vlookup, but on the second run it will look up a specific string, which I will provide.
For example,
If LOOKUPfound = Application.WorksheetFunction.VLookup(LOOKuptext, LOOKuprange, LOOKupcolumn, Not LOOKupmatch) does not return a value rerun the Vlookup, but change the value.
LOOKUPfound = Application.WorksheetFunction.VLookup(alternate lookup text, LOOKuprange, LOOKupcolumn, Not LOOKupmatch)
The reason why I am doing this is because I am matching data from two separate departments and sometimes these departments use the wrong names for the investments. However, I know what they mean and I can easily match them.
I hope I am making sense
Here is an example of my code.
Currently, when a match is not found, the code goes to an error handler. However, I want to perform another check before I go to the error handler. I want the code to re-run the Vlookup, but on the second run it will look up a specific string, which I will provide.
For example,
If LOOKUPfound = Application.WorksheetFunction.VLookup(LOOKuptext, LOOKuprange, LOOKupcolumn, Not LOOKupmatch) does not return a value rerun the Vlookup, but change the value.
LOOKUPfound = Application.WorksheetFunction.VLookup(alternate lookup text, LOOKuprange, LOOKupcolumn, Not LOOKupmatch)
The reason why I am doing this is because I am matching data from two separate departments and sometimes these departments use the wrong names for the investments. However, I know what they mean and I can easily match them.
I hope I am making sense
Here is an example of my code.
Code:
Sub schedule_9()
Dim i As Integer
Dim LOOKuppath As String
Dim LOOKupfile As String
Dim LOOKupWS As String
Dim LOOKuptext As String
Dim LOOKuprange As Range
Dim LOOKupcolumn As Integer
Dim LOOKupmatch As Boolean
Dim LOOKUPfound As Long
Dim ErrorValue As Integer
Dim subfile As String
Dim Subsheet As String
LOOKuppath = "Z:\Accounting\Reporting\SI15\15Q1\15Q1 SI\SI 1051\Sch 9\"
LOOKupfile = "a - 1q15 Schedule 9 Workbook .xlsx"
LOOKupWS = "Pivot Schedule 9"
subfile = "2q15 GRE SI Submission file.xlsm"
Subsheet = "SCH 9 Detail"
Workbooks.Open Filename:=LOOKuppath & LOOKupfile, UpdateLinks:=False
Workbooks(LOOKupfile).Sheets(LOOKupWS).Activate
' LOOK UP VALUES
For i = 2 To 19
LOOKuptext = Workbooks(subfile).Sheets(Subsheet).Cells(i, 1)
Set LOOKuprange = Sheets(LOOKupWS).Columns("A:G")
LOOKupcolumn = 2
LOOKupmatch = True
'On Error Resume Next
'DO LOOK UP
LOOKUPfound = Application.WorksheetFunction.VLookup(LOOKuptext, LOOKuprange, LOOKupcolumn, Not LOOKupmatch)
'store error value and turn off error handling
ErrorValue = Err
On Error GoTo 0
'check error value to see if match found
If ErrorValue <> 0 Then
MsgBox "Can 't find " & LOOKuptext
Else
Workbooks(subfile).Sheets(Subsheet).Cells(i, 20) = LOOKUPfound
End If
Next i
End Sub