Use value in cell as index for row in Excel SUM function - excel

Given the two worksheets below, I want to put a SUM in Column F for each row but instead of SUMMING the values in Sheet1, I need to use the cell value in Sheet1 as the row number in Sheet2 and use those cell values in the SUM. For example, the SUM for Bill should be SUM(Sheet2! B1,C6,D4,E3) = 200.
Sheet1
A B C D E F
Bill 1 6 4 3 200
Sue 2 1 3 2 450
Mary 3 2 2 1 550
Joe 4 3 1 4 150
Alice 5 4 25
Bob 6 5 0
Sheet2
A B C D E
1 100 200 50 400
2 50 100 25 200
3 25 50 0 100
4 0 25 0 50
5 0 0 0 0
6 0 0 0 0
This is just a sample spreadsheet; in the real one there are already 40 columns with more being added as necessary.

In Sheet1!F1:
=SUMPRODUCT(N(OFFSET(Sheet2!$A$1,B1:E1-1+(B1:E1-1<0)*(2^20-1),COLUMN(B1:E1)-1)))
Copy downwards as necessary.
The cells Sheet2!1048576:1048576 should not have content. Because those cells will be referenced if the row number given in Sheet1 is 0 or empty.

With the data to be summed in Sheet2!A1:E6, use this formula in Sheet1's first row's column F.
=SUM(INDEX(Sheet2!B:B, B2)*SIGN(B2), INDEX(Sheet2!C:C, C2)*SIGN(C2), INDEX(Sheet2!D:D, D2)*SIGN(D2), INDEX(Sheet2!E:E, E2)*SIGN(E2))
A zero or blank in Sheet1 would mean the entire column but that is multiplied by the SIGN function of that cell's value. Anything multiplied by zero equals zero and that will not impact the overall sum.
      
Using the INDEX function with the cell's value supplying the row_num parameter allows you to avoid volatile¹ functions that would negatively impact the calculation lag of the workbook.
¹ Volatile functions recalculate whenever anything in the entire workbook changes, not just when something that affects their outcome changes.

Related

How to ensure list of numbers in excel column continues from the last non zero number

Imagine 3 columns, Column A and Column B and Column C.
Column A is a date column.
Column C is lookup column that looks up a value from 1 - 100 from a different table based on the date.
Column B is continuation of values from 0 from the first date value and continues with the value until the value in column c changes.
How do you formulate column B so it automatically keeps the last non zero value until another value in column C appears?
Column A
Column B
Column C
01/01/2021
0
0
02/01/2021
0
0
03/01/2021
0
0
04/01/2021
20
20
05/01/2021
20
0
06/01/2021
20
0
07/01/2021
50
50
08/01/2021
50
0
09/01/2021
50
0
Either
=IFERROR(LOOKUP(2,1/(C$1:C1<>0),C$1:C1),0)
Or;
=XLOOKUP(TRUE,C$1:C1<>0,C$1:C1,0,0,-1)
Or, if these values are always bigger then the last one:
=MAX(C$1:C1)
You can do it as a spill formula in Excel 365 too:
=LET(range,C1:C9,
seq,SEQUENCE(ROWS(range)),
XLOOKUP(seq,IF(range>0,seq),range,0,-1)
)

Formula to calculate a sum of differences and count them

I have the following Excel spreadsheet:
A B C D E F G H
1 Sales 500 700 600 450 550 600 500
2 Helper Row (Differences) 40% -14% -25% 22% 9% -17%
3 Helper Row (Counts) 0 0 1 0 0 0
4 Count Result 1
In Row 1 you can see the sales over different periods. In Row 2 the difference between the sales are displayed. (e.g. formula in C2=C1/B1-1).
In Row 3 the formula indicates 2-Following-Periods in which in total the sales drop by >-20%.
In the case above this applies to cell E3 because the sales in cell D2 drop by -14% and in the next period in cell E2 by -25% which makes a total drop of those two periods by -39%.
The formula I use in Row 3 is for example E3=IF(SUM(D2:E2)<-0.2,1,0).
Eventually, I use a sum function in cell B4 (B4=SUM(B3:H3)) to count how often the above described criteria is met.
All this works perfectly so far. However, my target now is to get rid of the Hepler Row 2 and Row 3.
Do you know a formula that gives me the count result which meets the above described criteria?
Assuming that your above demonstrated numbers are in cell B1:H1, you can use following formulae to achieve result without helper cells.
Array formula (CTRL+SHIFT+ENTER and not just ENTER)
=SUM((((C1:H1-B1:G1)/B1:G1)<-0.2)+0)
Or for normal ENTER
=SUMPRODUCT((((C1:H1-B1:G1)/B1:G1)<-0.2)+0)

Find maximum of row, return column name

I have four rows and six columns of random numbers between 1 and 10. The headers atop are named A through F accordingly. I want to populate a range (A1:A6) on another sheet with the maximum number for each row. That is easy with the MAX function. However, in a another range (B1:B6), I want to put the column name to which this number belongs.
An HLOOKUP() won't work because a maximum value in one row is likely not unique number across the entire sheet. I am thinking a MATCH INDEX type function, but my understanding of those functions, especially in conjunction, is poor.
A B C D E F
1 0 2 10 9 8
9 3 7 6 9 10
10 3 0 2 1 4
9 4 7 8 6 3
Assuming your array is in Sheet1 and the columns are labelled, please try in another sheet, copied down to suit (to Row4 since there are only four rows of numbers in your data):
=INDEX(Sheet1!A$1:F$1,MATCH(MAX(Sheet1!A2:F2),Sheet1!A2:F2,0))
This will return only the first column label from a row where the maximum for that row occurs more than once.

Excel Find Value next to max value

If I do a simple formula such as
=MAX(J:J)
and I have a table such as
1 2
2 10
3 45
4 1
5 144
I would expect to see my cell = 144
is there a way for me to get the result 5 (as in the column to the left of the max?)
So if you want the value from column I try this formula
=INDEX(I:I,MATCH(MAX(J:J),J:J,0))
MATCH finds the relevant row number then INDEX gives you the value in column I from that same row

VBA to add missing numbers in a column and add a zero in next to it in column B

Can anyone write a code for the following task.
I have a series of numbers in column A and an associated value in column B.
e.g
A B
1 144
2 33
5 4
6 56
8 1
I need to run a macro that add in the missing consecutive numbers in column A and adds an associated zero value in column b.
e.g
1 144
2 33
3 0
4 0
5 4
6 56
7 0
8 1
can anyone help
If you can present the information on a different sheet, you don't need vba.
On the 2nd sheet, put your numbers in column A, using autofill to get the progression, or use =A1+1 as a formula to create the progression.
in column B, use =IFERROR(VLOOKUP(A1,Sheet1!$A$1:$B$5000,2,FALSE),0) to find the number, or to put 0 if no number is found
(use =IF(ISERROR(VLOOKUP(A1,Sheet1!$A$2:$B$5,2,FALSE)),0,VLOOKUP(A1,Sheet1!$A$2:$B$5,2,FALSE)) if your version of excel does not support ISERORR)

Resources