Sum an index value based on input from several columns

alcatrax

New Member
Joined
Jun 30, 2011
Messages
2
Hi,

I have the following simplified excel data:

column1 column2 column3 column4
row2 20 3 108 66
row3 17 54 99 208
row4 15 17 5 57
row5 9 98 44 3

I am looking for a formula that first matches the row number based on a given value (for example '3') and returns the value of the first column. Next, it will sum all the matches found. In this example with 3. It will find '3' in columns 2 and 4 and returns 20 + 9 = 29. Is that possible?

So far I have: =SUM(INDEX(H2:AF16,MATCH(M25;Stage2;0);1))

But this will only look in the range set by Stage2 (which is only one column). Extending this range to multiple columns does not work.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
UDF. Parameters description:
1. Rng - range with digits.
2. DigitToFind - self-describing. :)

Type in any cel, for example: =FirstDigits(A2:D6,3)

Code:
[COLOR="Blue"]Function[/COLOR] FirstDigits(Rng [COLOR="Blue"]As[/COLOR] Range, DigitToFind [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Double[/COLOR]) [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Double[/COLOR]

    [COLOR="Blue"]Dim[/COLOR] i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR], j [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] arr [COLOR="Blue"]As[/COLOR] Variant, res [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Double[/COLOR]
    
    Application.Volatile
    arr = Rng
    
    [COLOR="Blue"]For[/COLOR] i = 1 [COLOR="Blue"]To[/COLOR] [COLOR="Blue"]UBound[/COLOR](arr, 1)
        [COLOR="Blue"]For[/COLOR] j = 1 [COLOR="Blue"]To[/COLOR] [COLOR="Blue"]UBound[/COLOR](arr, 2)
            [COLOR="Blue"]If[/COLOR] arr(i, j) = DigitToFind [COLOR="Blue"]Then[/COLOR]
                res = res + arr(i, 1)
            [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
        [COLOR="Blue"]Next[/COLOR]
    [COLOR="Blue"]Next[/COLOR]
    
    FirstDigits = res

[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Function[/COLOR]
 
Upvote 0
Thanks for the reply. The sumproduct did it! Apparently no need for match, index, offset etc etc. Just a simple sumproduct will do......*sigh*

Thanks again.

As for the VBA function: that is worth studying for future reference!
 
Upvote 0

Forum statistics

Threads
1,224,558
Messages
6,179,512
Members
452,920
Latest member
jaspers

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