top of page

Excel VBA - Multiple If Conditions

  • Writer: Eighplus Admin
    Eighplus Admin
  • Mar 24, 2022
  • 2 min read

Updated: Apr 27, 2025

Here is an example:


When the income < 15,000, the marginal tax rate is 0%.

When income is between 15,000 and 50,000 , the marginal tax rate is 20%

When income is between 50,000 and 100,000 , the marginal tax rate is 30%

When income is above 100,000 , the marginal tax rate is 40%


Now I want to use this example to show how to write VBA code in “Multiple If Conditions”.


First of all, we find “button” in the menu , then click it and draw a frame in excel sheet, which is “Command Button 1”, of course you can rename it. Click this button to open editor.


In most cases, we start from “Sub”. The following content is the name that you make for this program, but it is better not to use key words, such as “if”, “select” as the name. Now we want to use “If Condition” to write the code. So we start with “If”, “Range (“E3”)” is the value in“E3”, we need to compare this value with given values, which are “15000”, “50000” and “100000” respectively. If the “E3” value is lower than 15000, then there is a “0” in F3. If 15000<= value in E3<=50000, then it is 20% in F3. We can notice that we do not write “15000<=” in line 4, since if it turns to line 4, it means the condition in line 2 is not met, the value in E3 must be larger than or equal to 15000. And we use “ElseIf” to represent the following conditions if the first one is not met. This is similar with Python If Conditions. In the end, we need to write “End If”, this “If ” refers to the “if” in second line instead of “ElseIf” in line 4, line6 and line 8, so there is only one “End If”. Then we just need to write down “End Sub” to end this code. Run this code, we can get 0.3 in F3.



Alternatively, we can use 'Case' statements.



FUNTERN Associate: Jin Wang

Comments


bottom of page