if-statementgoogle-sheetsmultiple-conditionscategorization

What is the correct syntax to classify ages into groups using IF statements in Google Sheets?


[Why am i getting "formula parse error" when I try to classify the ages (column H) into groups using the following formula? And is there a better way? Thanks for your assistance:

=IF (H19<20, “0-19”, IF ((H19>=20 AND H19<40), “20-39”, IF ((H19>=40 AND H19<60), “40-59”, IF ((H19>=60 AND H19<70), “60-69”, IF (H19>=70, ">= 70", “WRONG”)))))

I was expecting to output the Age column into strings based on my category definitions.


Solution

  • The portions that you have formatted as (H19>=20 AND H19<40) should be changed to AND(H19>=20, H19<40). Your final formula should then be:

    =IF(H19<20, “0-19”,
      IF(AND(H19>=20, H19<40), “20-39”,
      IF(AND(H19>=40, H19<60), “40-59”,
      IF(AND(H19>=60, H19<70), “60-69”,
      IF(H19>=70, ">= 70", “WRONG”)))))
    

    Alternatively:

    =IFS(OR(NOT(ISNUMBER(H19)),H19<0), "WRONG",
      H19<20, "0-19",
      AND(H19>=20, H19<40), "20-39",
      AND(H19>=40, H19<60), "40-59",
      AND(H19>=60, H19<70), "60-69",
      H19>=70, ">= 70")