• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Index Match (error)

ashish mehra

Active Member
Hi,

I am scratching my head all of the yesterday to find out the error in the code.

Can somebody check the attached file & let me know why my code is giving results when I select 1)Name as A in cell F7; 2) Subject as Eng in cell G7 & on the other hand I get no result when I change the Subject to Maths.:eek:

I tried using add watch with no luck.

Regards,
AM:)
 

Attachments

  • VBA for 2 Criterias TestResults.xlsm
    14.7 KB · Views: 5
Hi:

The error is because the index formula should be an array formula in your vba it is not taken as array but as normal formula matching 1 row and 1 column, where as you need to match 2 columns to get the desired result, both the name and the subject.

Thanks
 
Try this code
Code:
Sub vba2criteria()
myName = [F7]
mySubject = [G7]
m = ActiveSheet.Evaluate("=INDEX(C2:C12,MATCH(F7&G7,A2:A12&B2:B12,0))")
[H7] = m
End Sub

Thanks
 
Hi Ashish ,

The formula suggested by Nebu will always work , but in your present case , if you want to make use of the structure which is shown , where the marks are sorted Student-wise and for each Student , Subject-wise , with every student having all subjects , then use the following for m :

m = Application.WorksheetFunction.Index([StMark], Application.WorksheetFunction.Match(myName, ([StName]), 0) + Application.WorksheetFunction.Match(mySubject, [StSubject], 0) - 1)

Narayan
 
Hi Ashish,

Just another option try using this also in your code:

m = Application.WorksheetFunction.SumIfs([StMark], [StName], myname, [StSubject], mysubject)


Regards,
 
Back
Top