HWL
Active Member
- Joined
- Dec 1, 2009
- Messages
- 462
Okay, I think I'm just not getting it. I'm really trying to do something real simple. I'm a master at Excel functions, not so good at VBA.
With functions, I use index and match instead of vlookup.
SAMPLE:
if (iserror(index(sheet1range,match(criteria, sheet1range, false),1)),"",index(sheet1range,match(criteria, sheet1range, false),1))
That would return blank if the criteria wasn't found or if matched, it would return the contents of any column row match on sheet1.
I want to do this in VBA but when it encounters no match it crashes.
Here is my VBA code. It crashes when there is no match. tempfile1 and tempfile2 are named earlier so that is not the issue.
With functions, I use index and match instead of vlookup.
SAMPLE:
if (iserror(index(sheet1range,match(criteria, sheet1range, false),1)),"",index(sheet1range,match(criteria, sheet1range, false),1))
That would return blank if the criteria wasn't found or if matched, it would return the contents of any column row match on sheet1.
I want to do this in VBA but when it encounters no match it crashes.
Here is my VBA code. It crashes when there is no match. tempfile1 and tempfile2 are named earlier so that is not the issue.
For x = 2 To srowend
If WorksheetFunction.IsError(WorksheetFunction.Index(tempfile2.Sheets("INS Variances").Range("m1:m500"), WorksheetFunction.Match(tempfile1.Sheets("INS Variances").Cells(x, 1), tempfile2.Sheets("INS Variances").Range("A1:A500"), False), 1)) Then
Else
mtest = WorksheetFunction.Index(tempfile2.Sheets("INS Variances").Range("m1:m500"), WorksheetFunction.Match(tempfile1.Sheets("INS Variances").Cells(x, 1), tempfile2.Sheets("INS Variances").Range("A1:A500"), False), 1)
MsgBox mtest
End If
Next x
If WorksheetFunction.IsError(WorksheetFunction.Index(tempfile2.Sheets("INS Variances").Range("m1:m500"), WorksheetFunction.Match(tempfile1.Sheets("INS Variances").Cells(x, 1), tempfile2.Sheets("INS Variances").Range("A1:A500"), False), 1)) Then
Else
mtest = WorksheetFunction.Index(tempfile2.Sheets("INS Variances").Range("m1:m500"), WorksheetFunction.Match(tempfile1.Sheets("INS Variances").Cells(x, 1), tempfile2.Sheets("INS Variances").Range("A1:A500"), False), 1)
MsgBox mtest
End If
Next x