JB, I would make the point that using INDEX in this form is generally regarded as being pretty slow - else we'd use all the time in preference to SUMPRODUCT.
I think also that the above would fail to return correct result if the value in A appeared multiple times but under different codes (ie the MATCH should be conducted against the concatenation of I & A rather than just A) ... I believe also that should any entries in A be blank on Sheet2 then you would get an #N/A error.
Adopting JB's approach in SUMPRODUCT form with the above taken into account I believe something along the lines of:
=SUMPRODUCT(--(Sheet2!$A$1:$A$7<>""),--(Sheet2!$I$1:$I$7=A6),--(MATCH(Sheet2!$I$1:$I$7&Sheet2!$A$1:$A$7&"",Sheet2!$I$1:$I$7&Sheet2!$A$1:$A$7&"",0)=ROW(Sheet2!$A$1:$A$7)))
should work... it would of course be easier to have a concatenation column at source, eg:
Sheet2!J1: =$A1&":"&$I1
copied down
Then
=SUMPRODUCT(--(Sheet2!$A$1:$A$7<>""),--(Sheet2!$I$1:$I$7=A6),1/COUNTIF(Sheet2!$J$1:$J$7,Sheet2!$J$1:$J$7&""))
which is as you can see a little less convoluted... but not much
Bookmarks