COUNTIF with absolute column, relative row in Google Sheets - google-sheets

I have this table and I want B1 and B2 to display the sum where D1, F1 and H1, and D2, F2 and H2 are bigger than 0, respectively. I will be inserting two columns to the left of C on a regular basis, and I don't want the column references in the formula to be updated when the columns are shifted, but if B2 becomes B3 when a row is added above it, I will need the formula to be updated for the row and check the values in D3, F3 and H3, or if I add rows below it, I want to be able to drag down and get the formula copied with incremental row values. I tried INDIRECT with and without &CELL("address", E2) but I couldn't get it to work as I wanted it.
A B (Sum) C D E F G H
1 Josh 3 Some number 6 Some number 4 Some number 3
2 Fiona 2 Some number 1 Some number 0 Some number 4

You can use OFFSET to get absolute column references, and COUNTIF to count if each referenced cell is bigger than 0:
=COUNTIF(OFFSET(B1,0,2),">0")+COUNTIF(OFFSET(B1,0,4),">0")+COUNTIF(OFFSET(B1,0,6),">0")

Not perfect, but try =SUM(OFFSET(B2,0,1,1,500)). That way you refer to the cell itself so whenever you add columns it's not affected. I used 500 arbitrarily since it's simple. Adjust as needed.

Related

how can compare two cells in google sheet with multiple value in every cells

how can compare two cells in google sheets with multiple value in every cells and if even one value in two cells are same its show me match but if in two cells we do not have any same value show me not match.
for example I have these cells in my google sheets:
a1:[1,2,3,4]
b1:5,6,7
a2:[10,12,14,16]
b2:18,14,20
for these cell it should compare a1 and b1 and print not match in c1 then for a2 and b2 print match in c2 because a2 and b2 have a same value 14
you can try this arrayformula in Column C
=MAP(INDEX(REGEXREPLACE(A:A,"\[|\]","")),B:B,LAMBDA(ax,bx,IF(ax="",,IF(ISERROR(FILTER(SPLIT(ax,","),(MATCH(SPLIT(ax,","),SPLIT(bx,","),0)))),"FALSE","TRUE"))))
-
For not matching values-
=TEXTJOIN(",",1,LAMBDA(x,y,FILTER(x,NOT(ISNUMBER(XMATCH(x,y)))))(FLATTEN(SPLIT(SUBSTITUTE(SUBSTITUTE(A1,"[",""),"]",""),",")),FLATTEN(SPLIT(B1,","))))
For matching values-
=TEXTJOIN(",",1,LAMBDA(x,y,FILTER(x,ISNUMBER(XMATCH(x,y))))(FLATTEN(SPLIT(SUBSTITUTE(SUBSTITUTE(A2,"[",""),"]",""),",")),FLATTEN(SPLIT(B2,","))))
Edit: To show TRUE/FALSE try-
=INDEX(LAMBDA(x,y,OR(ISNUMBER(XMATCH(x,y))))(FLATTEN(SPLIT(SUBSTITUTE(SUBSTITUTE(A1,"[",""),"]",""),",")),FLATTEN(SPLIT(B1,","))))
Another approach, based on creating an intermediate 2D array of IFs for each pair of cells to be compared then summing the result; given the columns for comparison in A1:A & B1:B then placing this formula in C1 will spill as many results as required:
=arrayformula(iferror(map(A1:A,B1:B,lambda(a,b,if(sum(n(split(a,"[],")=flatten(split(b,"[],")))),"Match","No match")))))

Adding the first digit(s) of a cell ending with specific letter

I have a sheet that has blocks of cells. I add them for a total if they don't contain any letter. Letters signify a specific variable or signifier code for tracking. I also want to add the cell value when a cell has an ending code letter. I've tried SUMIF, Substitute, SumProduct and a few others.
For a while I've used the following which worked till the S value changed to another number than "8" which then gave the wrong sum from the range.
=if(countif(D64:Q64,"*S")=0,"",((countif(D64:Q64,"*S"))*8))
In the cell range I have 5 variable groups ending in a letter (A, H, S, C and R) and one group not ending in any letter.
The formula I use to add the cells not containing a letter but have a value is this
=IF(SUM(AA64,(SUMIF(D65:Q65,"<>")),-(COUNTIF(D65:Q65,">=0")*8))>24,24,(SUM(AA64,(SUMIF(D65:Q65,"<>")),-(COUNTIF(D65:Q65,">=0")*8))))
which adds the table data plus the previous lines table data but does not exceed 24.
H is a set value of 6 and doesn't change.
Does anyone know how to add the value of the cells that contain a specific letter?
N (number)
A
S
C
R
H
8.5
H
8A
2S
9
3C
0.5R
17.5
8
2
3
0.5
6
I'd prefer it be a formula usable in Google Sheets because that is where this data is.
Solution:
You can use this formula below column "N" then drag right until below column "H".
=SWITCH(H$1,"N",SUM($A2:$G2),"H",6,SUM(IFERROR(ARRAYFORMULA(VALUE(LEFT($A2:$G2,FIND(H$1,$A2:$G2)-1))),0)))
This is a combination of three formulas:
If the row above is N, sum A2 to G2. This will ignore all the strings.
If the row above is H, output 6.
Else, get the number to the left of the defined suffix, set all others to 0, and get the sum.
Output:
References:
Extract number from string in Google Sheets
An alternative could be to use
=SUMPRODUCT(A3:G3, isnumber(A3:G3))
to compute the sum of the cells with numbers only, and to use
=sum(filter(substitute($A$3:$G$3, I$2,)+0, regexmatch($A$3:$G$3, I$2)))
under the 'A' and drag to the right
Examples

Create a column with numbers from 0 to 1000 in increments of 10

I am new to google sheets and am unaware of how to create a sequence of numbers in a column. I want to create a column with numbers from 0 to 1000 in increments of 10
Method 1 (formula)
Copy the following formula =(ROW()-1)*10
Select cells A1:A100
Paste the formula.
Method 2 (fill down series):
Write 0 on A1
Write 10 on A2
Write 20 on A3
Select A1:A3
Click on the bottom right corner of A3, then drag down until A100.
=ARRAYFORMULA({0; ROW(A1:A100)&0})
or:
=ARRAYFORMULA({0\ ROW(A1:A100)&0})
if you want real numbers then use:
=ARRAYFORMULA({0; VALUE(ROW(A1:A100)&0)})
or:
=ARRAYFORMULA({0\ VALUE(ROW(A1:A100)&0)})

Google Spreadsheet: E2-F2 even if i move the rows

I'm having an issue with google spreadsheets. I want to... have Cell C2 always contain the difference between cell E2 and F2 even if I move the row (i don't want the formula to adapt to that change of rows, the formula should still be E2-F2); so let's say E2 contains 5 and F2 contains 3, C2 would display 2. So let's say I switch row E and row F. Right now the result displayed would still be 2, and the formula in cell C2 would've changed to F2-E2. What I want is the formula not to change, and the result displayed would be -3.
=INDIRECT(E2)-INDIRECT(F2) didn't work for me - it gave me a reference error.
The INDIRECT function expects a string parameter. So you would need to write
=INDIRECT("E2")-INDIRECT("F2")
But the better way to solve your problem would be to use absolute references
=$E$2-$F$2

I need to add the total of every 3 rows

I need to add the total of every 3 rows.... so I need total of rows 1- 3 then total of 4-6 then total of 7-9 and so on and need to do this for over 400 rows...
Assuming the data you wish to sum is in Column A and the summed data will be in Column B (with headers for both columns) you can do the following:
On B4 enter =SUM(A2:A4)
Then select B2:B4 and drag to copy the contents down your range of data.
This will give you the sum of every three rows.
Assuming the list starts at A1, you can use the following starting at B3 and carrying it down the list. Then set B1 and B2 to 0 and filter out all zeros.
=IF(MOD(ROW(),3)=0,SUM(A1,A2,A3),0)
This determines if the row number is divisible evenly by 3, if it is it returns the sum of that row and the previous 2, otherwise it returns zero.
If you have a header row, you can change it to and set zeros for B2 and B3 and start this on B4
=IF(MOD(ROW()-1,3)=0,SUM(A2,A3,A4),0)

Resources