Simple loop match

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.
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
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Possibly

Code:
If IsError(Application.Index(tempfile2.Sheets("INS Variances").Range("m1:m500"), Application.Match(tempfile1.Sheets("INS Variances").Cells(x, 1), tempfile2.Sheets("INS Variances").Range("A1:A500"), False), 1)) Then
 
Upvote 0
Possibly

Code:
If IsError(Application.Index(tempfile2.Sheets("INS Variances").Range("m1:m500"), Application.Match(tempfile1.Sheets("INS Variances").Cells(x, 1), tempfile2.Sheets("INS Variances").Range("A1:A500"), False), 1)) Then

Seems to work, weird since it is just a chage in appl error and vba iserror
Thanks much
 
Upvote 0
ah okay, this is going to be a real useful piece of code for the work I do. Thanks much
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top