How to add 5 cells consecutively after an interval of 5 cells in a row of 200 cells? - excel

I have a row of 200 cells. I have to add/average first five cells (A:E), and then take a break of 5 cells and then add/average second five cells (K:O) and so on till the end of the row. How can I do this?
I have tried doing it manually. Actually, I can do this manually but wanted to know if I can do this automatically.

I did a test making some faking data like this:
It's just a bunch of numbers from A1 to CB1. 80 numbers in total. 5 first numbers are 1, then next 5 are 2, next 5 numbers are again 1, then next 5 numbers are again 2, and so on.
This mean that there are 80 numbers, where 40 are 1 and 40 are 2. I want to get the average of first five cells (A:E), and then take a break of 5 cells and then average second five cells (K:O) and so on till the end of the row. In other words, I want to get the average of the 40 cells that contains a 1 value, and it should return a 1.
For this, I've used an array formula:
=AVERAGE(IF(VALUE(RIGHT(COLUMN(A1:CB1);1))<6;IF(VALUE(RIGHT(COLUMN(A1:CB1);1))>0;A1:CB1)))
NOTE: Because this is an array formula, it must be inserted pressing ENTER+CTRL+SHIFT instead of
only ENTER, or it won't work!
How this works?
You want to sum/average only values that are in columns where last digit of column number is 1 to 5, this means columns 1,2,3,4,5,11,12,13,14,15,21,22,23,24,25, and so on. So this works this way:
The part that says COLUMN(A1:CB1) will get an array of column numbers.
RIGHT(COLUMN(A1:CB1);1) will get last digit of each column number, but as text
VALUE(RIGHT(COLUMN(A1:CB1);1)) will convert that last digit into a number.
Then with both IFS, we get an array of only those values where last digit of column number is >0 and <6, and we get the average. I get as result of my average 1 and it's true, because the average of 40 times 1 is equal to 1.
Hope this works for you. You can adapt this easily to make it work with 200 cells.

For example:
In A2 put:
=IF(MOD(COLUMN(),10)=1,AVERAGE(INDEX(1:1,,COLUMN()):INDEX(1:1,,COLUMN()+4)),"")
Drag right.

You can use SUMPRODUCT to add the amounts in the columns and divide by 100:
=SUMPRODUCT(--ISEVEN(INT((COLUMN(A1:GR1)-1)/5)),A1:GR1)/100
if you do not always have 200 numbers you can make the 100 more dynamic with another SUMPRODUCT:
=SUMPRODUCT(--ISEVEN(INT((COLUMN(A1:GR1)-1)/5)),A1:GR1)/SUMPRODUCT(ISEVEN(INT((COLUMN(A1:GR1)-1)/5))*(ISNUMBER(A1:GR1)))

this function have CTRL+SHift+Enter and drag this function until your last data IF(OR((RIGHT(COLUMN(A5),1)+0)={1,2,3,4,5}),A5,--FALSE)
You Find The Columns only which end (1,2,3,4,5) , in last. You use Sum function For Add.

Related

Count last 10 rows containing a word

I have a column of wins, "Yes" or "NO" in each cell. I want to Count the number of "yes" in the last 10 rows.
One issue is that not every row has an answer yet but I want the blank row that has other data to be counted for the last 10. So I'm using another column that has data as the count reference. I'm not super formula savvy but have learned a lot lately. This is the formula I'm trying to use but It does not give me the desired result.
=COUNTIF(OFFSET(M1,COUNT(L:L)-10,0),"Yes")
OFFSET is Volatile use INDEX(MATCH())
MATCH(1E+99,L:L)
Will find the last row with a number in it. Then point that at column M in the index:
INDEX(M:M,MATCH(1E+99,L:L))
Which now returns the last row in Column M where there is a number in column L
to get the starting cell we subtract 9:
INDEX(M:M,MATCH(1E+99,L:L)-9)
then it just a mater of treating them like the beginning and end of a range:
INDEX(M:M,MATCH(1E+99,L:L)-9):INDEX(M:M,MATCH(1E+99,L:L))
Then wrap that in the COUNTIF:
=COUNTIF(INDEX(M:M,MATCH(1E+99,L:L)-9):INDEX(M:M,MATCH(1E+99,L:L)),"Yes")
You're really close! I think you want =COUNTIF(OFFSET(M1,COUNT(L:L)-1,0,-10),"Yes")
Row offset: If you have 25 rows of data, COUNT(L:L) will give you 25. You want to offset by 24, since we're starting from M1, so subtract 1.
Column offset: 0
Row span: We want to span backwards 10 rows, so -10
Source: https://exceljet.net/formula/average-last-5-values

If a value appears less than 3 times in every 10 cells of a column then replace these values with another value

In a column I have more than 50000 values. Many of these are missing and are designated as "NA". I would like to run through every 10 rows of this column and if there less than 3 missing values, i.e. 1 or 2 "NA" to replace them with zero "0".
I have struggled a lot with this and the furthest I have managed to get is this:
=IF(AND(COUNTIF((OFFSET(A$1,(ROW()-ROW(A$1))*10,,10,)),$A$1)<3,A1="NA"),0,A1)
This is not correct.
With the aforementioned formula I get this result (which is not what I want):
The desired result would be this:
Put this in B1 and copy down:
=IF(OR(A1<>"NA",COUNTIFS(INDEX(A:A,INT((ROW(1:1)-1)/10)*10+1):INDEX(A:A,INT((ROW(1:1)-1)/10)*10+10),"NA")>2),A1,0)

Excel - function to find the highest sum in a table using each row and column only once

I've got a table in excel with 10 rows and 10 columns.
The table contains 100 different values between 1 and 3.
I want to find the highest sum of 10 values using only 1 value from each row and 1 from each column.
Do u guys know a function that finds the highest sum? - I've tried to do i manually, but there are to many combinations!
Hope it makes sense.
Thanks in advance:)
My solution builds on what I wrote in the comment, i.e. you first take the maximum value in the 10x10 array, then the maximum in the 9x9 array (excluding the row/column of the first maximum), etc. My solution tries not to do everything in one formula, but I add a few helper columns, and a bit more helper rows (it is fast and dirty, but it works and is easily audited/understandable). You always can do this on a separate worksheet which you could hide if needed.
The screenshot above goes from cell A1 till Y31.
The key formulas:
3.55 is the result of =MAX(B2:K11)
The first gray cell is =IFNA(MATCH($M12;B2:B11;0);""), and you drag this 9 cells to the left. This tries to find a match with the max result in each column of the table;
The 10 left of the 3.55 is =MATCH(TRUE;INDEX(ISNUMBER(P12:Y12);0);0) , and gives the column number of the max value.
The 2 next to the 10 is =INDEX(P12:Y12;N12) and gives the row number of the max value.
The 1 in cell B12 is =IF(OR(B$1=$N12;$A12=$O12);0;1), and creates a 10x10 matrix with a row and column with zeroes where the previous max value was found.
Then you multiply this with the preceding matrix and create a new 10x10 matrix below (enter {=B2:K11*B12:K21} array formula (ctrl+shift+enter) in B22-K31
You then copy/paste rows 12 till 31 9 times below
The 23.02 is the total sum =SUM($M$12:$M$211) from all 10 maximum values and is the result you are looking for. The 10 is just a check with =COUNT($M$12:$M$211)

Excel: Merge two columns into one column with alternating values

how can I merge two columns of data into one like the following:
Col1 Col2 Col3
========================
A 1 A
B 2 1
C 3 B
2
C
3
You can use the following formula in column D as per my example. Keep in mind to increase the $A$1:$B$6 range according to your data.
=INDEX($A$1:$B$6,INT((ROWS(D$2:D2)-1)/2)+1,MOD(ROWS(D$2:D2)-1,2)+1)
Result:
Thank you to #Koby Douek for the answer. Just an addition--if you are using Open Office Calc, you replace the commas with semi-colons.
=INDEX($A$1:$B$6;INT((ROWS(D$2:D2)-1)/2)+1;MOD(ROWS(D$2:D2)-1;2)+1)
Expanding #koby Douek's answer to more columns and explaining some of the terms
Original Code for 2 columns to 1 alternating
=INDEX($A$1:$B$6,INT((ROWS(D$2:D2)-1)/2)+1,MOD(ROWS(D$2:D2)-1,2)+1)
$A$1:$B$6 Defines the columns and rows to source the final set of data from, the $s are only present to keep the formula from changing the columns and rows selects if it is copied and pasted or dragged.
To extend to work on any values you dump into the columns instead of having to expand the range every time it should be amended to $A:$B or A:B so you can easily copy it to other sets of columns and create new merges, but it will also give the 1st value in every column as one of the alternating values so if you instead have headers you would be able to do this by instead using a large number so $A$1:$B$99999 or A$1:B$99999 if you want to past and move the columns ymmv which is better by situation.
lets assume you are fine including the values in the 1st row
This changes the formula to
=INDEX($A:$B,INT((ROWS(D$2:D2)-1)/2)+1,MOD(ROWS(D$2:D2)-1,2)+1)
Now on to D$2:D2
This is the row that is being used to calculate the difference between the current row the formula is in (D2) and the reference row (D$2) The important thing to make sure you do is to set the reference row number to the 1st row you will be putting values in, so if your 1st row is a header in the sort column you will use the 2nd row as the reference, if your values in the combined column D begin on the 3rd row then the reference row would be D$3
Since I like the more general form where the 1st row isn't a header row I'll use D$1:D1 but you could still mix source rows without headers into a combined row with a header of as many rows as you like just by incrementing that reference row number to be the 1st row where your values should begin.
This changes the formula to
=INDEX($A:$B,INT((ROWS(D$1:D1)-1)/2)+1,MOD(ROWS(D$1:D1)-1,2)+1)
Now INT((ROWS(D$1:D1)-1)/2)+1 and MOD(ROWS(D$1:D1)-1,2)+1
INT returns an integer value so any decimal places are dropped, it essentially functions like rounding down to the nearest whole number
MOD functions by returning the remainder of a division, it's result will be a whole number between 0 and n-1 where n is the number we are dividing by. (eg: 0/3=0; 1/3=1; 2/3=2; 3/3=0; 4/3=1 ... etc)
So -1)/2)+1 and -1,2)+1
the first value is again the difference between the current row and the reference row. but D$1:D1 is going to be the count of the rows, which is 1 so we have to correct for the rows count starting at 1 instead of 0 which would throw off our calculations, so both are using the -1 to reduce the count of the rows by 1
in the case of /2 and ,2 both are because we are dividing by 2 in the first statement it's a normal division by 2 /2 in the modulus statement it's an argument of the Mod function so ,2
finally we need to add 1 using +1 to correct for the index's need to have a value series which begins at 1.
INT((ROWS(D$2:D2)-1)/2)+1 is finding the row number to select the value from.
MOD(ROWS(D$1:D1)-1,2)+1 is finding the column number to select the value from
Thus we can change /2 and ,2 to /3 and ,3 to do this with 3 columns
This yields:
=INDEX($A:$B,INT((ROWS(D$1:D1)-1)/3)+1,MOD(ROWS(D$1:D1)-1,3)+1)
So maybe that's the confusing way to look at it but it's closer to how my mind works on it. Here is an alternative view:
=INDEX([RANGE],[ROW_#],[COLUMN_#]) returns the value from a range of rows and columns
Using the example:
=INDEX($A:$B,INT((ROWS(D$1:D1)-1)/3)+1,MOD(ROWS(D$1:D1)-1,3)+1)
[RANGE] = $A:$B this is the range of source columns.
[ROW_#] = INT((ROWS(D$1:D1)-1)/3)+1
INT([VALUE_A])+1 returns an integer value so any decimal places are dropped. Then adds one to it. we add one to the value because the result of the next steps will be 1 less than the value we need.
[Value_A] = (ROWS(D$1:D1)-1)/3
ROWS(D$1:D1) returns the number of rows in the Range to the current row in the results column, we use D$1 to designate the row number where the values in the results column begin. D1 is the current row in the results column giving us a range from the source row, allowing us to count the rows. we have to subtract 1 from this value using -1 to get the difference between the source and current. This is then divided by /3 because we have three columns we want to look through in this example so we only change rows when the result is divisible by 3. the INT drops any decimal places as mentioned so it only increments when cleanly divisible by 3.
[COLUMN_#] = MOD(ROWS(D$1:D1)-1,3)+1
MOD([VALUE],[Divisor])+1 returns the remainder of the value when divided by the divisor.
Using the example:
MOD(ROWS(D$1:D1)-1,3)+1
In this case we still divide by 3 but it's an argument to the MOD function, we still need to count the number of rows and subtract 1 before dividing it, this will return a 0, 1, or 2 for the column, but as above we are shifted backwards by 1 as the column numbers begin with the number 1, so as before we must add 1
And here we add column A and D
two different formulas depending on if you add the formula to an odd row or an even row.
https://1drv.ms/x/s!AncAhUkdErOkguUaToQkVkl5Qw-l_g?e=5d9gVM
Odd Start row
=INDEX($A$2:$D$9;ROUND(ROW(A1)/2;0);IF(MOD(ROW()-ROW($A$2);2)=1;4;1))
Even Start row
=INDEX($A$2:$D$9;ROUND(ROW(A1)/2;0);IF(MOD(ROW()-ROW($A$1);2)=1;4;1))
What is A1 in the picture is the cell directly above your first data cell.
If you want to place it on a different sheet you just add the sheet name:
=INDEX(MySheet!$A$2:$D$9;ROUND(ROW(MySheet!A1)/2;0);IF(MOD(ROW()-ROW(MySheet!$A$2);2)=1;4;1))
=INDEX(MySheet!$A$2:$D$9;ROUND(ROW(MySheet!A1)/2;0);IF(MOD(ROW()-ROW(MySheet!$A$1);2)=1;4;1))

Using COUNTIFS for a series of values at once

Working a step higher then COUNTIFS, I appose a challenge to write a formula without VBA code. The basic data is combined from 1000s of rows with:
Column A: rows with values from 1 to 3
Column B: rows with values from 1 to 250.
For this purpose lets say, we are looking at all cells of value "1" in column A, that suit value "5" in column B. To find all matches, we'd use COUNTIFS command.
1 1
2 5
1 5
1 7
1 10
3 45
2 12
1 2
2 1
=COUNTIFS(A1:A9;1;B1:B9;5)
The answer here is 1.
Next thing, the "5" in column B belongs to a group, e.g. group from 1 to 9. What would the best way be, to count all the matches in this example, so that for all "1"'s in column A, we'd have to find all matches with values from 1 to 9 in column B?! In the upper example that would result in "4". The obvious solution is with a series of IF commands, but that's unefficient and it easy to make a mistake, that get's easily overseen.
=COUNTIFS(A1:A9;1;B1:B9;"<="&9)
Works only as the upper limit. If I give the third criteria range and condition as ">="&1 it does not work - returns 0.
Gasper
Where the data is in A1:B9, using a lookup table in D1:E10 with letters A-J in column D and numbers 0 to 9 in column E and the following formula in B11 referencing letters entered in A11 and A12:
=COUNTIFS(A1:A9,1,B1:B9,">="&VLOOKUP(A11,$D$1:$E$10,2,FALSE),B1:B9,"<="&VLOOKUP(A12,$D$1:$E$10,2,FALSE))
works, changing the letters in A11 and A12 gives the correct count according to what they correspond to in the looku in D1:E10.
When you say give third criteria range do you mean:
=COUNTIFS(A1:A9;1;B1:B9;"<="&9,B1:B9;">=1")
If so then try:
=COUNTIFS(A1:A9;1;B1:B9;AND("<="&9,;">=1"))
ie have two conditional ranges with the second range having both conditions combined with AND()
Maybe what you want(ed) is:
=COUNTIFS(A:A;1;B:B;">=1";B:B;"<=9")
Almost there. I noticed that three criteria ranges and conditions work only if I use "=" sign in a condition. As soon as I use
=COUNTIFS(A1:A9;1;B1:B9;"<="&9,B1:B9;">=1")
it returns 0. My goal is to eventualy replace the number in a condition with a VLOOKUP command, so the final equation should be smth like
=COUNTIFS(A1:A9;1;B1:B9;"<="&VLOOKUP(...),B1:B9;">=VLOOKUP(...)")
But the "<" and ">" signs mess with this. Still looking for a solution.
This is my entire line, if it offers any further indication. The AND() commands is at the end - and it still results in 0
=COUNTIFS(INDIRECT(CONCATENATE("baza!$";SUBSTITUTE(ADDRESS(1;MATCH("card_type_id";baza!$A$1:$AAA$1;0);4);"1";"");"$2:$";SUBSTITUTE(ADDRESS(1;MATCH("card_type_id";baza!$A$1:$AAA$1;0);4);"1";"");"$15000"));IF(C6="računska";1;0);INDIRECT(CONCATENATE("baza!$";SUBSTITUTE(ADDRESS(1;MATCH(IF($C$4="CC_SI_klasifikacija";"building_classification_id";0);baza!$A$1:$AAA$1;0);4);"1";"");"$2:$";SUBSTITUTE(ADDRESS(1;MATCH(IF($C$4="CC_SI_klasifikacija";"building_classification_id";0);baza!$A$1:$AAA$1;0);4);"1";"");"$15000"));AND("<="&VLOOKUP($C$5;$K$203:$N$223;4;FALSE);">="&VLOOKUP($C$5;$K$203:$N$223;3;FALSE)))

Resources