From the course: Excel: Learning VBA

Unlock the full course today

Join today to access over 24,000 courses taught by industry experts.

Execute code conditionally using an If…Then statement

Execute code conditionally using an If…Then statement - Microsoft Excel Tutorial

From the course: Excel: Learning VBA

Execute code conditionally using an If…Then statement

- [Instructor] So far, most of this course's example files have contained subroutines or other code that run every time you call them. There will be times, though, when you want parts of your subroutines to run when a condition is true. You can use if then statements to execute code conditionally. To demonstrate, I will use the sample file named 03_04_IfThen, and you can find it in the Chapter Three folder of the exercise files collection. In this workbook I have a single worksheet, and in cell C5 I have the amount of a sale. And the question, which is stated in cell E4, is whether that amount qualifies for a bonus. You can do that fairly straightforwardly using an if function in Excel. So in cell E5, I'll type equal, and then if. And then in the parentheses, the logical test, and I will test to see if C5 is greater than or equal to 1400, so one four zero zero. I'll type comma. If it's true, I want to display yes.…

Contents