Search code examples
excelexcel-formulaexcel-2010excel-2013

Logic for IF statement


I have data in Excel like this:

   A
 1|id
 2|2
 3|4
 4|8
 5|12
 6|16
 7|20
 8|24
 9|28
10|32
11|36
12|40

I want to populate the value in another column depending upon the following criteria:

if cell A2>0 && cell A2<=8 
    return 1
else if cell A2>8 && cell A2<=16 
    return 2
else if cell A2>16 && cell A2<=24 
    return 3
else if cell A2>24 && cell A2<=32 
    return 4
else return 5

I was only able to do it for one IF condition:

=IF(AND(A2>0,A2<8),1)

How can I add logic for else if?


Solution

  • Like I said in my comments above, you don't need vba for this.

    The IF has the following syntax

    =If(Condition, Do If True, Do If False)
    

    So in your case

    Condition = AND(A2>0,A2<8)
    Do If True = 1
    Do If False = Nothing?
    

    So Do If False is the place where you will set the next condition. For example

    =If(Condition, Do If True, If(Condition, Do If True, Do If False))
    

    And So on...

    Something like this I am taking the 2nd condition (>8,<16) as an example. Change as applicable.

    =IF(AND(A2>0,A2<8),1,IF(AND(A2>8,A2<16),2,Next condition goes here))