VBA Conditional Statements

  1. Visual Basic Code Examples For Excel Free

When this code is used with the sample table, cell A4 will be selected. How to Select the Blank Cell at Bottom of a Column of Contiguous Data. To select the cell below a range of contiguous cells, use the following example: ActiveSheet.Range('a1').End(xlDown).Offset(1,0).Select When this code is used with the sample table, cell A5 will be selected. VBA code for Excel can only be written inside Excel using the VBA IDE. VBA projects are stored as part of the Excel file and cannot be loaded into Visual Studio. However, you can write VSTO (Visual Studio Tools for Office) managed add-ins for Excel using Visual Studio. The following MSDN page covers both developing with VBA and VSTO.

How To Save An Excel File As PDF Using VBA: Basic Code Examples (Examples #1 And #2) Now that you are familiar with the ExportAsFixedFormat VBA method, let's take a look at some code examples. For purposes of this example, I have prepared a sample Excel workbook. This workbook contains 2.

The main Excel VBA Conditional Statements are the If ... Then statement and the Select Case statement. Both of these evaluate one or more conditions and, depending on the result, execute specific sections of code.

The two Conditional Statement types are discussed individually below.


The Visual Basic If ... Then Statement

The If ... Then statement tests a condition and if it evaluates to True, executes a specific section of code. If the condition evaluates to False, a different section of code is executed.

The syntax of the If ... Then statement is:

IfCondition1Then
Code to be executed if Condition1 evaluates to True
ElseIfCondition2Then
Code to be executed if Condition2 evaluates to True
Else
Code to be executed if none of the previous conditions evaluate to True
End If

In the above If statement, you can add as many ElseIf conditions as you require. Alternatively, the ElseIf and the Else parts of the conditional statement can be omitted if desired.

In the example below, an If ... Then statement is used to color the current active cell, depending on the value of the cell contents.

Basic
If ActiveCell.Value < 5 Then
ActiveCell.Interior.Color = 65280 ' Color cell interior green
ElseIf ActiveCell.Value < 10 Then
ActiveCell.Interior.Color = 49407 ' Color cell interior orange
Else
ActiveCell.Interior.Color = 255 ' Color cell interior red
End If

Note that, in the above example, the If statement stops once it has satisfied a condition. Therefore, if the ActiveCell value is less than 5, the first condition is satisfied and so the cell is colored green. The If ... Then statement is then exited, without testing any further conditions.


For further information on the VBA If ... Then statement, see the Microsoft Developer Network website.


The Visual Basic Select Case Statement

The Select Case statement is similar to the If ... Then statement, in that it tests an expression, and executes different sections of code, depending on the value of the expression.

The syntax of the Select Case statement is:

Select CaseExpression
CaseValue1CaseValue2
Actions if Expression matches Value2
Case Else
Actions if expression does not match any of listed cases
End Select

In the above code, the Case Else part of the conditional statement is optional.

In the following example, the Select Case statement is used to color the current active cell, depending on the value of the cell contents:

Select Case ActiveCell.Value
Case Is <= 5
ActiveCell.Interior.Color = 65280 ' Color cell interior green
Case 6, 7, 8, 9
ActiveCell.Interior.Color = 49407 ' Color cell interior orange
Case 10
ActiveCell.Interior.Color = 65535 ' Color cell interior yellow
Case Else
ActiveCell.Interior.Color = 255 ' Color cell interior red
End Select

The above example illustrates different ways of defining the different Cases in the Select Case statement. These are:

Case Is <= 5This is an example of how you can test if your expression satisfies a condition such as <= 5 by using the keyword Case Is
Case 6, 7, 8, 9This is an example of how you can test if your expression evaluates to any one of several values, by separating the possible values by commas
Case 10This is an example of the basic test of whether your expression evaluates to a specific value
Case ElseThis is an example of the 'Else' condition, which is executed if your expression hasn't matched any of the previous cases

Note that as soon as one case in the Select Case statement is matched, and the corresponding code executed, the whole Select Case statement is exited. Therefore, the code will never enter more than one of the listed cases.


For further information on the VBA Select Case statement, see the Microsoft Developer Network website.

Go To Excel VBA Tutorial Part 6 - Loops
Return to the Excel VBA Tutorial Page

So you needed to do something in Excel and after a bit of searching online, the solution you found involves some VBA code (visual basic for applications). If you’ve never used VBA and don’t know where to put the code and how to run it then follow this step by step guide to using the VBA code you found online.

Step 1: Enable the Developer tab in the Ribbon.

By default the Developer tab is hidden and you will need to enable it to use in the ribbon.

Excel
  1. Go to the File tab.
  2. Click Options section.
  3. Click the Customize Ribbon section.
  4. Check the Developer box.
  5. Press the OK button.

Step 2: Open the visual basic editor.

Now you should see the Developer tab in your Excel ribbon and you can open the visual basic editor (VBE) from the ribbon.

  1. Go to the Developer tab.
  2. Press the Visual Basic button in the code section.

Alternatively, you can open the VBE with the Alt + F11 shortcut.

Step 3: Insert a module into the current workbook.

On the left hand side of the VBE you should see the Project Explorer side bar. This will list all your open workbooks and VBE objects associated with them. If you don’t see the project explorer, go to View > Project Explorer or press Ctrl + R.

  1. In the Project Explorer, find the workbook you want to use the VBA code in and right click on it.
  2. Click Insert from the menu.
  3. Click Module from the sub-menu.
Visual Basic Code Examples For ExcelExamplesBasic

A new item will appear in the project explorer called Module1 and the previously grey space will contain a white section. This is the module code window.

  1. Click on Module1.
  2. Paste your code in the module.

Step 4: Run your code.

Now you can run your code.

  1. Go to the Developer tab.
  2. Press the Macros button from the Code section.
  3. Select your code from the Macro window.
  4. Press the Run button

Alternatively, to open the Macro window you can use the Alt + F8 shortcut.

Note: For saving your workbook.

You will need to save your workbook as a macro-enabled file type if you want to use this code the next time you open the workbook. These file types all support macros (VBA).

Visual Basic Code Examples For Excel Free

  1. Excel Macro-Enabled Workbook (*.xlsm)
  2. Excel Binary Workbook (*.xlsb)
  3. Excel 97-2003 Workbook (*.xls)