Splitting a column by consecutive zeros - excel

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)))

Related

How to use SUMIF in an array formula to not sum blank rows

Hi I am using the following array formula to sum rows AC-AG , I would like to modify it so that it does not sum if an entire row is blank. How would I do this using an array formula since this spreadsheet is tied to a google forum and gets updates often.
I am using this formula at the top of the column.
={"# Boxes of Household Items"; ARRAYFORMULA(SUMIF(IF(COLUMN(AC1:AG1),ROW(AC2:AG)),ROW(AC2:AG),AC2:AG))}
Example Below
The Formula adds up AC through AG and puts them in AR.
I want the formula to not put a zero in AR5 since the entire row is blank
In the image above I have numbers in various cells from columns A:E, then I have a formula in cells F1:F3.
Row 1 has numbers for all cells.
Row 2 has numbers for some cells.
Row 3 has no numbers.
To show the sum when all/some of the cells contain numbers, I would do:
=IF(COUNT(A1:E1)>0,SUM(A1:E1),"")
We check IF there is at least one number using COUNT(A1:E1)>0. If there is at least one number, we then show the sum using SUM(A1:E1) otherwise we just show nothing, which is the "" part.
For you the formula would be this in AR2:
=IF(COUNT(AC2:AG2)>0,SUM(AC2:AG2),"")

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

How can I expand a dynamic range with blanks between cells?

I want to define a dynamic range that will expand the rows and columns of my range whenever a new row or column is inserted. My current formula does not want to expand to cell $T$13. My headers start in row $M$7. How can I adjust my formula?
Formula Being Used
=OFFSET(Sheet1!$M$8,0,0,COUNTA(Sheet1!$M:$M),COUNTA(Sheet1!$1:$1))
I need my range to expand to cell $T$13
Right now, your formula counts the number of text values in column M.
That is not a robust approach because column M contains only five text values, but columns S and T have many more values.
If you don't know which column may have the most number of entries, you can introduce a helper cell in each column that counts the number of entries below. I suggest you insert a new row 2. In column M, for example, put a formula in M2
=counta($M$3:M$99999)
Copy that formula across to column T.
Next you can evaluate which of the columns has the largest number
=max(M2:T2)
This can be plugged into your original formula like this:
=OFFSET(Sheet1!$M$8,0,0,max(M2:T2),COUNTA(Sheet1!$1:$1))
So now, instead of just looking at how many rows are in column M, the formula uses the maximum number of rows in the columns M to S.
You can now hide row 2 if it upsets your worksheet design.
Edit: the mere count of text values with CountA will ignore blank cells and will return incorrect results. You really need a formula to find the row number of the last populated cell in each column.
This should really be a new question, but here goes
If the column has number values you can use
=MATCH(99^99,B5:B999,1)
If the column has text values you can use
=MATCH("zzz",C5:C999,1)
Adjust your ranges accordingly.
I ended up using the solution mentioned by #tevlyn.
In range$M2:$T2
I have the follolwing formula =IFERROR(MATCH(99^99,M$8:M$999,1),0).
I've added IFERROR because my data doesn't always have data stretched to $T2.
I then defined my range in name manager using:
=OFFSET(Sheet3!$M$8,0,0,MAX(Sheet3!$M$2:$T$2),COUNTA(Sheet3!$1:$1))
This still works even if there are blanks in between the range.

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.

Check the number of unique cells in a range

I have an excel sheet.
Under column E, I have 425 cells with data. I want to check if the same data (i.e. text inside the cell) is repeated anywhere else in any of the remaining 424 cells under column E. How do I do this?
For example, in E54 I have
Hello Jack
How would I check this value to see if it was in any other of these cells?
You could use
=SUMPRODUCT(1/COUNTIF(E1:E425,E1:E425))
to count the number of unique cells in E1:425
An answer of 425 means all the values are unique.
An answer of 421 means 4 values are duplicates of other value(s)
Use Conditional Formatting on all the cells that will highlight based on this formula:
COUNTIF(E:E,E1) <> 1
This is based on the column being E, and starting on E1, modify otherwise.
In Excel 2010 it's even easier, just go into Conditional Formatting and choose
Format only unique or duplicate values
If you have to compensate for blank cells, take the formula supplied above by #brettdj and,
Adjust the numerator of your count unique to check for non-blanks.
Add a zero-length string to the COUNTIFS's criteria arguement.
=SUMPRODUCT((E1:E425<>"")/COUNTIF(E1:E425,E1:E425&""))
Checking for non-blank cells in the numerator means that any blank cell will return a zero. Any fraction with a zero in its numerator will be zero no matter what the denominator is. The empty string appended to the criteria portion of the COUNTIF is sufficient to avoid #DIV/0! errors.
More information at Count Unique with SUMPRODUCT() Breakdown.
This formula outputs "unique" or "duplicates" depending if the column values are all unique or not:
{=IF(
SUM(IF(ISBLANK(E1:E425),0,ROW(E1:E425)))
=
SUM(IF(ISBLANK(E1:E425),0,MATCH(E1:E425,E1:E425,0)))
,"unique","duplicates")}
This is an array formula. You don't type the enclosing {} explicitly. Instead you enter the formula without {} and then press cmd-enter (or something else if not a Mac - go look it up!) If you want to split your formula over multiples lines for readability, use cmd-ctrl-return on a Mac.
The formula works by comparing two SUM() results. If they are equal, all the nonblank entries (numeric or text) are unique. If they are not equal there are some duplicates. The formula does not tell you where the duplicates are.
The first sum is what you get by adding up the row numbers of every non-blank entry.
The second sum does a lookup of each nonblank entry using MATCH(). If all entries are unique, MATCH() finds each entry at its own position, and the result is the same as the first sum. But if there are duplicate entries then a later duplicate will match an earlier duplicate and the later duplicate will contribute a different value to the sum, and the sums won't match.
You might have to adjust this formula:
if you want cells containing "" to count as blank, then use LEN(...)=0 for ISBLANK(...). I suppose you could put other tests in there if you wanted, but I have not tried that.
if you want to test an array not starting at row 1, then you should subtract a constant from ROW(...).
if you have a huge column of cells, you might get integer overflow when computing this sum. I don't have a solution to that.
It's a shame that Excel does not have an ISUNIQUE() function!
This may be a simpler solution. Assume column A contains data in question. Sort on that column. Then, starting in B2 (or first non-blank cell, use the following formula:
=IF(A2=A1,1,0).
Than sum on that column. When sum = 0, all values are unique.
highlight E and on the home tab select conditional formatting > Highlight Cell Rules > Duplicate Values...
It will then highlight everything that is repeated.

Resources