Multiple nested if blocks in google spreadsheets formulas

拈花ヽ惹草 提交于 2020-01-04 04:24:26

问题


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)


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/35164826/multiple-nested-if-blocks-in-google-spreadsheets-formulas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!