Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple nested if blocks in google spreadsheets formulas

Im trying to write a nested if statement like this pseudo code :

=IF(h4=1, "CORRECT", IF(h4=2, "CORRECT"), IF(h4=3, "CORRECT"), IF(h4=4, "CORRECT")) 

But im getting the following error, if i write out the code about and add an if statement each time. ie. start with only the first if block and then add more testing each time, it breaks when i add the third statement.

Wrong number of arguments to IF. Expected between 2 and 3 arguments, but received 4 arguments.

Is there a better way to nest if blocks in google spreadsheets ?

Ive made a google spreadsheet of the issue here : https://docs.google.com/spreadsheets/d/1MBOmaTNI5C_spSVudCQPeanHtoFr36kglg9BXUeAZxU/edit#gid=0

(the above code is only an example of nesting IF blocks, not the actual issue im trying to solve)

like image 842
sam Avatar asked Sep 21 '26 18:09

sam


2 Answers

The error was actually just a simple syntax issue - the placement of your parentheses - here is the correct:

=IF(H4=1, "CORRECT", IF(H4=2, "CORRECT", IF(H4=3, "CORRECT")))

I also fixed it on your spreadsheet.

Every if statement must have 3 parts essentially, so if(this, then this, else this) so when nesting, the else this part of the formula is the next condition..

=IF(h4=1, "CORRECT", IF(h4=2, "CORRECT", IF(h4=3, "CORRECT", IF(h4=4, "CORRECT")))) 
like image 90
Aurielle Perlmann Avatar answered Sep 28 '26 22:09

Aurielle Perlmann


Short answer

Instead of nesting multiple IFs, try to use a formula that doesn't require that. Considering the example provided, one alternative is the following:

=IF(AND(h1>=1,h4<=4),"CORRECT")

Explanation

If it's possible, avoid function nesting as it could make a formula harder to read and debug.

like image 34
Rubén Avatar answered Sep 28 '26 20:09

Rubén



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!