Question : Sum several items based on individual criteria for each item

Hello!  I am trying to sum several items based on individual criteria for each item.  For example:
If [Item1]=6[Item1Qty]  PLUS If [Item2]=6[Item2Qty]  PLUS If [Item3]=6[Item3Qty]  
Basically, if either Item 1,Item 2 or Item 3 = 6, then I want to add together their respective quantities. Heres what Im currently using, but its not working properly:
= Sum(IIf([Item1]=6,[Item1 Qty],IIf([Item2]=6,[Item2 Qty],IIf([Item3]=6,[Item3 Qty],IIf([Item4]=6,[Item4 Qty],IIf([Item5]=6,[Item5 Qty],IIf([Item6]=6,[Item6 Qty],IIf([Item7]=6,[Item7 Qty],IIf([Item8]=6,[Item8 Qty],IIf([Item9]=6,[Item9 Qty],0))))))))))
Can anyone help?

Answer : Sum several items based on individual criteria for each item

Hi,

you cannot nest the IIFs, because if Item1 is 6 you would sum "6", otherwise you would sum the rest and so on.
If you really want to solve that on this way, try it with this:

=IIf([Item1] = 6, [Item1 Qty], 0) + IIf([Item2] = 6, [Item2 Qty], 0) + IIf([Item3] = 6, [Item3 Qty], 0) + IIf([Item4] = 6, [Item4 Qty], 0) + IIf([Item5] = 6, [Item5 Qty], 0) + IIf([Item6] = 6, [Item6 Qty], 0) + IIf([Item7] = 6, [Item7 Qty], 0) + IIf([Item8] = 6, [Item8 Qty], 0) + IIf([Item9] = 6, [Item9 Qty], 0)

It would be better to create a VBA function to loop through the numbered elements and add them. At the end you can insert the function into the field where you want the output.

Cheers,

Christian
Random Solutions  
 
programming4us programming4us