SUMPRODUCT with (space) Intersection operator

jackms

New Member
Joined
Dec 28, 2005
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
I have a range name that consists of non-contiguous columns.

I can use the (space) Intersection operator with functions like SUM and AVERAGE , but get #VALUE when I try it with SUMPRODUCT.

Example: if range myrange consists of A:A,C:C
1635177423212.png


=SUM(myrange 3:3)=26
But =SUMPRODUCT(myrange 3:3,myrange 6:6)=#VALUE!

Note it works fine when myrange is contiguous (e.g. if myrange would consist of A:B, then the above SUMPRODUCT formula returns 226)

What can I do for this situation?
 
Thanks. But that's not giving me the correct result.
In my original example: if range myrange consists of A:A,C:C
1635194825420.png

=(PRODUCT(myrange 3:3)+PRODUCT(myrange 6:6))/SUM(myrange 3:3) = (PRODUCT(3,23)+PRODUCT(6,26))/SUM(3,23) = (69+156)/26 = 8.653846154
but what I want is (PRODUCT(3,6)+PRODUCT(23,26))/SUM(3,23) = (18+598)/26 = 23.69230769
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Maybe it works using the rarely used area_num parameter of the INDEX function

Something like

Pasta1
ABCDEF
111121AreasResult
221222123,69231
3313232
441424
551525
661626
771727
881828
991929
10102030
Plan1
Cell Formulas
RangeFormula
F2F2=SUMPRODUCT(INDEX(myrange,3,1,N(IF(1,CHOOSE({1,2},E2,E3)))),INDEX(myrange,6,1,N(IF(1,CHOOSE({1,2},E2,E3)))))/SUM(myrange 3:3)


Syntax
INDEX(reference, row_num, [column_num], [area_num])

M.

 
Upvote 0
If that is not what you want then your question has been wrong from the start, the purpose of sumproduct is to multiply the items in a row, then add the total of the rows together. What you are now asking for is the reverse of that (effectively productsum if such a thing existed).

The result of my formula before division is 225, which is 1 different from what you said it should be in post 1 (which I took to be a simple mental arithmetic error) the figures that you are now saying it should return are nowhere near your original example.

Please check your requirement carefully and clarify exactly what is needed before I waste time looking for a likely impossible solution.
 
Upvote 0
Maybe it works using the rarely used area_num parameter of the INDEX function
Unlikely Marcelo, it has already been established that the number of columns in myrange (areas for index) can be variable, that is assuming that the information provided was correct, a fact that I am losing faith in.
 
Upvote 0
Unlikely Marcelo, it has already been established that the number of columns in myrange (areas for index) can be variable
What about this
myrange2 is gray area (A1:A10, C1:C10, F1:F10)

Pasta1
ABCDEFGHI
111121314151AreasResult
221222324252145,36709
3313233343532
4414243444543
551525354555
661626364656
771727374757
881828384858
991929394959
10102030405060
Plan2
Cell Formulas
RangeFormula
I2I2=SUMPRODUCT(INDEX(myrange2,3,1,N(IF(1,CHOOSE({1,2,3},H2,H3,H4)))),INDEX(myrange2,6,1,N(IF(1,CHOOSE({1,2,3},H2,H3,H4)))))/SUM(myrange2 3:3)


M.
 
Last edited:
Upvote 0
If that is not what you want then your question has been wrong from the start, the purpose of sumproduct is to multiply the items in a row, then add the total of the rows together. What you are now asking for is the reverse of that (effectively productsum if such a thing existed).
I sincerely apologize if I wasn't clear. In the following
1635219416995.png

the formula =SUMPRODUCT(A3:C3,A6:C6) = (PRODUCT(3,6)+PRODUCT(23,26)) = 616.
My issue is that in my spreadsheet, column B also has data, so I need to skip column B when doing SUMPRODUCT.

(When I said "if myrange would consist of A:B, then the above SUMPRODUCT formula returns 226", then it would be the equivalent of =SUMPRODUCT(A3:B3,A6:B6) which in the spreadsheet in my original message =226)
 
Upvote 0
What about this
myrange2 is gray area (A1:A10, C1:C10, F1:F10)

Pasta1
ABCDEFGHI
111121314151AreasResult
221222324252145,36709
3313233343532
4414243444543
551525354555
661626364656
771727374757
881828384858
991929394959
10102030405060
Plan2
Cell Formulas
RangeFormula
I2I2=SUMPRODUCT(INDEX(myrange2,3,1,N(IF(1,CHOOSE({1,2,3},H2,H3,H4)))),INDEX(myrange2,6,1,N(IF(1,CHOOSE({1,2,3},H2,H3,H4)))))/SUM(myrange2 3:3)


M.
Ingenious! I never knew about using N in that way. But I don't think this will solve my problem. I don't know in advance how many areas there will be. So if myrange2 would only have 2 areas, then the formula returns #VALUE.
 
Upvote 0
What about a user-defined function?

VBA Code:
Function WA(rng As Range, Optional FirstRow As Long = 3, Optional LastRow As Long = 6) As Double
  Dim rA As Range, col As Range
  Dim prod As Double, sm As Double
 
  For Each rA In rng.Areas
    For Each col In rA.Columns
      prod = prod + col.Cells(FirstRow).Value * col.Cells(LastRow).Value
      sm = sm + col.Cells(FirstRow).Value
    Next col
  Next rA
  WA = prod / sm
End Function

myRange is columns A and C:D. I have assumed myRange consists of entire columns.
Green cells were just for my benefit while checking.

jackms.xlsm
ABCDEFG
111121314130.57627119
2212223251
3313233361
4414243471
5515253581
6616263691
77172737101
88182838111
99192939121
1010203040131
Sheet1
Cell Formulas
RangeFormula
G1G1=WA(myRange)
 
Upvote 0
What about a user-defined function?

VBA Code:
Function WA(rng As Range, Optional FirstRow As Long = 3, Optional LastRow As Long = 6) As Double
  Dim rA As Range, col As Range
  Dim prod As Double, sm As Double
 
  For Each rA In rng.Areas
    For Each col In rA.Columns
      prod = prod + col.Cells(FirstRow).Value * col.Cells(LastRow).Value
      sm = sm + col.Cells(FirstRow).Value
    Next col
  Next rA
  WA = prod / sm
End Function

myRange is columns A and C:D. I have assumed myRange consists of entire columns.
Green cells were just for my benefit while checking.

jackms.xlsm
ABCDEFG
111121314130.57627119
2212223251
3313233361
4414243471
5515253581
6616263691
77172737101
88182838111
99192939121
1010203040131
Sheet1
Cell Formulas
RangeFormula
G1G1=WA(myRange)
Thank you! Great idea! But for my purpose, the workbook can't use VBA. It has to be a xlsx workbook.
 
Upvote 0
Playing around with @Marcelo Branco's earlier suggestions, I've managed to get 2 working formulas. The first will only work with office 365 / excel 2021. The second should work back as far as 2007 if the information on microsoft support is correct, however I have not been able to test it in anything other than 365. It is often the case with formulas like this that some additional persuasion in the form of N(IF({1} or similar is needed with older versions.
Excel Formula:
=LET(s,SEQUENCE(AREAS(myrange)),SUMPRODUCT(INDEX(myrange 3:3,,,s),INDEX(myrange 6:6,,,s)))
Excel Formula:
=SUMPRODUCT(INDEX(myrange 3:3,,,ROW(INDIRECT("1:"&(AREAS(myrange))))),INDEX(myrange 6:6,,,ROW(INDIRECT("1:"&(AREAS(myrange))))))
Both formulas return a result of 616 using the clarified test criteria.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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