Excel Guides

If/End If structure in Excel

If/End If structures are one of the most important tools in Excel. They allow you to make decisions based on certain conditions, and then take different actions depending on the result of those decisions.

For example, let's say you have a list of numbers in column A, and you want to highlight all the numbers that are greater than 10. You could use an If/End If structure like this:

If A1 > 10 Then 
    Range("A1").Interior.ColorIndex = 3 
End If 

This code says "if the number in cell A1 is greater than 10, then change the color of that cell to red." You can then copy and paste this code into all the other cells in column A, and it will only change the color of the cells that contain a number greater than 10.

If/End If structures can also be used to make more complex decisions. For example, let's say you have a list of names in column A, and you want to put all the names that start with "A" into one group, all the names that start with "B" into another group, and so on. You could use an If/End If structure like this:

If Left(A1, 1) = "A" Then 
    Range("A1").Value = "Group 1" 
ElseIf Left(A1, 1) = "B" Then 
    Range("A1").Value = "Group 2" 
ElseIf Left(A1, 1) = "C" Then 
    Range("A1").Value = "Group 3" 
End If

Move beyond 

Excel

Get started with Causal today.
Build models effortlessly, connect them directly to your data, and share them with interactive dashboards and beautiful visuals.