Problem : VBA Vlookup error 2042
I have been having trouble with getting my vlookup to work in vba.
I am trying to return a value from a table in another spreadsheet; I manually did the vlookup then used the code generated to produce the range. Below is the vba I have been using:
actualrate.Value = Application.VLookup(Vr1, Range(“‘[Test Rates.xls]Rates’!$A$3:$F$18”), 3, False)
actualrate returns = error 2042 but when, as I mentioned earlier, I did this lookup manually using the same code it worked correctly?!
I have also tried the code using the following variation
Set rg = Range(“‘[Nest Rates.xls]Rates’!$A$3:$F$18”)
actualrate.Value = Application.VLookup(Vr1, rg, 3, False)
which returns the same error…
when I have tried:
actualrate.Value = [an actual value I know is in the table]
then the code works as expected…but I have another workbook using the same formula and it works correctly!
I have made sure that the code in the range is looking in the correct place and i have tried it with and without the logical test at the end, so now I am at a bit of a loss…
I look forward to hearing from you chaps…
Solution: VBA Vlookup error 2042
PS If you are going to do VLookups in code, I suggest you add an error check anyway – something like:
varData = Application.VLookup(Vr1, Range(“‘[Test Rates.xls]Rates’!$A$3:$F$18”), 3, False)
If Not IsError(varData) Then
actualrate.Value = varData