Substract last cell in row from first cell with number - excel

I have the following Excel sheet
In column J i need the final difference between the first cell in the row and the last cell (with a number).
Numbers can appear from column C until column I. Numbers do not always start in column C and do not always end in column I, but there are never empty cells in between.
Basically i need to subtract the value in the first cell with a number from the last cell with a number. The last value in the range from C-I minus the first value in that range with the result being displayed in J.
I filled in column J manually for now, however I would like to do it with formula.

If the numbers are always ordered from smallest to largest, you could simply do this:
=MAX(C2:I2)-MIN(C2:I2)
If not, things become a bit more difficult. Here's another solution that should work for non-ordered entries:
First, add an empty column to the right of Totaal.
Second, add seven columns with the following contents:
=IF(ISBLANK(C2),M2,C2)
=IF(ISBLANK(D2),N2,D2)
...
Third, add another empty column.
Fourth, add seven columns with the following contents:
=IF(ISBLANK(C2),S2,C2)
=IF(ISBLANK(D2),T2,D2)
...
Totaal can then be calculated with
=Z2-L2

Related

Summing the Last n Cells in a row, excluding blanks, and Shifts when Numbers Inputed

So I got the basics of the formula down, but it doesn't shift when I input new numbers.
The formula takes the last six numbers in the row, excluding blank cells, and sums them. I'm looking for the summation to change, but keep the previous calculations.
Here it takes the last six and changes all of them. This is tough because it involves noninputs for some cells. Here's what I have. The 43 is in cell B7 for reference. The initial summation formula is
=IF(H$7<>"", SUM(OFFSET($B$7,0,COUNT($B$7:$AA$7)-1,,-6)), G$6)
Any quick fixes to the formula that I can do? Remember that there is a possibility of blank cells in the future (beyond when 41 is entered).
To sum the last 6 non-blank entries in a row, you can use the non-volatile formula:
=SUM(INDEX(B1:XFD1,-1+AGGREGATE(14,6,1/ISNUMBER(B1:XFD1)*COLUMN(B1:XFD1),{1,2,3,4,5,6})))
Assumes formula might be in A1
If formula not in the same row as the range to be summed, you could use a whole row reference (eg 1:1)
Note the -1 in the columns argument
This compensates for the difference between the INDEX into the array and the column number. Since the array starts in Column B (2nd column) we need to subtract 1.
If the array started in Column E (5th column) we would need to subtract 4.
If the array referred to the entire row, there would be no need to subtract.
If there are less than six numbers, the formula will return a #NUM! error. You could compensate for this by wrapping the formula within an IFERROR function, or you could test specifically with COUNT(arr) > 5

Increment by 2 for n rows, increment by 4 once and repeat when referencing data from one sheet to another

thank you for taking the time to look at this question.
I'm looking for an equation that can easily take the numerical values from Sheet 1 (the first picture) which has 2 blank cells in between values for four values and then has 4 blank cells and then the other four values. I'm not sure if I am making sense but hopefully the picture I have attached helps.
Notice 2 blank rows between first 4 rows with values (Rows 2-11) and same between rows 16 and 25.
Also notice the 4 blank rows between the two sets of values.
For me, this is repeated for 700 values, same set up of 2 blank rows for 4 sets of values and then 4 blank rows and then four sets of values with 2 blank rows. I'm sure there is an easier way to do this.
I'm trying to recreate Sheet 2 from Sheet 1 using an equation. Is this possible?
Apologies in advance, English isn't my first language.
If the numbers are going to start in B2 and the intervals and offset staggers are static then,
=INDEX(B:B, 2+(ROW(1:1)-1)*3+INT((ROW(1:1)-1)/4)*2)
If the first number is in S6 then,
=INDEX(S:S, 6+(ROW(1:1)-1)*3+INT((ROW(1:1)-1)/4)*2)
Put this in D2:
=IFERROR(INDEX(Sheet1!B:B,AGGREGATE(15,6,ROW(Sheet1!$B$2:INDEX(Sheet1!B:B,MATCH("ZZZ",Sheet1!A:A)))/(Sheet1!$B$2:INDEX(Sheet1!B:B,MATCH("ZZZ",Sheet1!A:A))<>""),ROW(1:1))),"")
And copy down till you get blanks.
This will return the numbers in order that they appear on sheet 1.
The Sheet1!$B$2:INDEX(Sheet1!B:B,MATCH("ZZZ",Sheet1!A:A)) set the data set bounds. This being an array type formula it needs to reference the smallest possible data set. This part finds the last cell in Column A and sets that as the extent of the data set so we do not do unnecessary iterations.
The MATCH part will return the last row that has text in it, if Column A has numbers then we need to change the "ZZZ" to 1E+99 to get the last row in column A with a number.
The AGGREGATE is working like a small in that it will create an array of row numbers and Errors. It will return ROW Numbers where (Sheet1!$B$2:INDEX(Sheet1!B:B,MATCH("ZZZ",Sheet1!A:A))<>"") return true. And an Error where it returns FALSE.
The second criterion 6 in Aggregate tells it to ignore the errors, so it is only looking at the returned row numbers.
The ROW(1:1) is a counter. As the formula is dragged down it will iterate to 2 then 3 and so on. This tells the Aggregate that you want the 1st then the 2nd then the 3rd and so on.
The chosen row number is then passed to the INDEX and the correct value is returned.
If your numbers are in order (smallest to largest like your example) or you want the output in order(smallest to largest) then you can use this simple equation in D2:
=IFERROR(SMALL(Sheet1!B:B,ROW(1:1)),"")
Then copy down till you get blanks.
Here is another formula you might use.
=INDIRECT(ADDRESS((INT((ROW()-ROW($A$2))/4)*14+ROW(A$2))+(MOD(ROW()-ROW($A$2),4)*3),COLUMN($A$2),1,1,"Sheet1"))
You can paste it to the first cell where you want the result and copy down.
Note that $A$2 is the cell from where all the counting starts. If your data start from A3 you can change the references accordingly. Note further that ROW($A$2) is long for 2. I chose this syntax to enable you to identify the meaning.
COLUMN($A$2), on the other hand, just identifies Column A as the source of the data to be lifted. Row 2 in this formula is insignificant. It's the A that counts. However, COLUMN($A$2) is long for just 1, meaning column No. 1, meaning A. Once you get your bearing in the formula you can replace COLUMN($A$2) with 1.

Splitting a column by consecutive zeros

I'm new to excel and I am struggling to do something very easy. I have a column of unsorted numbers. I want to set all of the numbers above the last two zeros in consecutive rows in the column to zero (and also in another column set all of the numbers below the first two consecutive zeros to zero).
For example, I have highlighted all of the cells in column A that are above the last two consecutive zeros. I want a method (VBA or formula) that will set these cells to zero and create the output in seen in column B.
Would anyone be able to help?
There are various ways of doing it but I would be inclined to use a helper cell (say D1) to work out the last row where that row and the next row contain a pair of zeroes:-
=MAX(IF((A1:A19+A2:A20)=0,ROW(A1:A19)))
(this is an array formula and must be entered with CtrlShiftEnter)
Then enter the following formula in B1:
=IF(ROW()<$D$1,0,A1)
If you had negative numbers as well as positive, this would be better in D1:-
=MAX(IF((A1:A19=0)*(A2:A20=0),ROW(A1:A19)))

Find the 2nd to last or nth to last value in a column

I want to find the 2nd to last or nth to last value in a column, I already have a formula that works perfectly for my needs for finding the last value and I'm hoping there is a way to modify it to allow for the nth to last value to be found but as of yet I cant figure that out. Here it is:
=LOOKUP(2,1/(1-ISBLANK(B:B)),B:B)
You can try the below formula (note: this is an array formula, you need to use Ctrl+Shift+Enter for it to work properly instead of Enter alone):
=INDEX(B:B,LARGE(IF(B:B<>"",ROW(B:B)),2))
Change the last 2 to 3 to get the 3rd from last non-blank value. The above formula works even if there are blanks in the columns between the non-blank values.
IF(B:B<>"",ROW(B:B)) basically fetches the row number for non-blank rows, then LARGE picks the largest, or 2nd largest, etc depending on the value used at the end, to get the nth row you are looking for.
INDEX just picks the value from that row.
=INDEX(B:B, COUNTA(B:B) -D1 +1, 1)
=OFFSET(B:B, COUNTA(B:B) -D1, 0, 1, 1)
Where D1 contains your value 'N'.
These will both return the Nth value from the bottom of column B, assuming that there are not any blank cells between B1 and the final row of your list.

Get non-zero value from current or previous row

I am working in Excel.
Suppose I have two columns. In first column, there are some zero and some non-zero values. The second column is blank.
If there is a non zero value in a cell of first column, the same value should be written in the next column. But if the value is a zero, then the previous non zero value in the first column should be written in the second column. So, in the end, the second column gets filled with all non zero values.
Could you help me to write the formula?
I think this is what you want. Please take a look:
https://docs.google.com/spreadsheets/d/183LFCPGfBnA0IEs4LKxk7iOgayxIFdCDaFZjFcwSU3M/edit?usp=sharing
Basically, First cell in second column, will check for zero in the first cell in first column:
=if(A1=0;"";A1)
Subsequent cells from second column will display the value of the cell above if the adjacent cell in the first column is zero:
=if(A2=0;B1;A2)
From there, is just formula extension down by the column.
Assuming you have your spreadsheet like this:
A1="Title"
A2=123
A3=0
A4=321
You could use this formula in cell B2, and copy it down:
B2=IF(A2=0, OFFSET(B2,-1,-1,1,1), A2)
Assuming your data/values start in the first column, here is an if statement that will handle it for you.
=IF(A2>0,A2,B1)

Resources