Your VLookup
statements will fail with the described error message if the VRange
object is nothing.
It's difficult to understand what you're trying to do but I will try to break it down. Here you have a loop that exits on the first instance of the CBool
expression returning True
.
For v = LBound(vWSs) To UBound(vWSs)
If CBool(Application.CountIf(.Sheets(vWSs(v)).Range("A2:ZA2"), mf_iA_TEXT)) Then
Set Mrange = .Sheets(vWSs(v)).Range("A2:ZA2")
Set Vrange = .Sheets(vWSs(v)).Range("A:ZA")
mf_iA = Application.Match(mf_iA_TEXT, Mrange, 0)
Exit For
End If
Next v
At this point assuming the loop is exited by the Exit For
statement, the VRange
object should exist and be a valid range, so the next loop should not fail:
For i = 2 To lastrow
wsMaster.Cells(i, 2) = Application.VLookup(wsMaster.Cells(i, 1), Vrange, mf_iA, 0)
Next i
HOWEVER if that first loop completes without ever "finding" the value mf_iA_Text
, then the VRange
object is not a valid range, and that would cause the error.
Suggest to revise by putting the second loop inside the first:
For v = LBound(vWSs) To UBound(vWSs)
If CBool(Application.CountIf(.Sheets(vWSs(v)).Range("A2:ZA2"), mf_iA_TEXT)) Then
Set Mrange = .Sheets(vWSs(v)).Range("A2:ZA2")
Set Vrange = .Sheets(vWSs(v)).Range("A:ZA")
mf_iA = Application.Match(mf_iA_TEXT, Mrange, 0)
For i = 2 To lastrow
wsMaster.Cells(i, 2) = Application.VLookup(wsMaster.
manpreet
Best Answer
2 years ago
I've written some code here, aided by the extremely helpful user Jeeped. Only problem is that I'm getting the error
Invalid Procedure Call or Argument
when the vlookup does not find a result. If it finds a result, it seems to be fine.I want it to just return #N/As if it can't find a result. Can you help me fix this? Also I have 500 columns to do this with so if there's a much easier way to write it to account for that, that'd be great. I'm still learning VBA. Thanks so much.