Visual Basic Code Examples In Excel

  1. Excel Vba Example
  2. Visual Basic Code Examples In Excel Using
  3. Visual Basic Code Examples For Excel Pdf

Open the Visual Basic Editor. In the Project Explorer, right-click the project name and select Import File. Navigate to the.bas file and click Open. Excel macro examples. One of the best ways to learn Excel VBA is by exploring code samples. Below you will find examples of very simple VBA codes that automate some basic operations. Now before I get into the Macro Example and give you the VBA code, let me first show you how to use these example codes. Using the Code from Excel Macro Examples. Here are the steps you need to follow to use the code from any of the examples: Open the Workbook in which you want to use the macro. Hold the ALT key and press F11. This opens the VB.

VBA Conditional Statements

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.

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
Code

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
-->

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The examples in this article use the Visual Basic methods listed in the following table.

The examples in this article use the properties in the following table.

How to Select a Cell on the Active Worksheet

To select cell D5 on the active worksheet, you can use either of the following examples:

How to Select a Cell on Another Worksheet in the Same Workbook

To select cell E6 on another worksheet in the same workbook, you can use either of the following examples:

Or, you can activate the worksheet, and then use method 1 above to select the cell:

How to Select a Cell on a Worksheet in a Different Workbook

To select cell F7 on a worksheet in a different workbook, you can use either of the following examples:

Or, you can activate the worksheet, and then use method 1 above to select the cell:

How to Select a Range of Cells on the Active Worksheet

To select the range C2:D10 on the active worksheet, you can use any of the following examples:

How to Select a Range of Cells on Another Worksheet in the Same Workbook

To select the range D3:E11 on another worksheet in the same workbook, you can use either of the following examples:

Or, you can activate the worksheet, and then use method 4 above to select the range:

How to Select a Range of Cells on a Worksheet in a Different Workbook

To select the range E4:F12 on a worksheet in a different workbook, you can use either of the following examples:

Visual

Or, you can activate the worksheet, and then use method 4 above to select the range:

How to Select a Named Range on the Active Worksheet

To select the named range 'Test' on the active worksheet, you can use either of the following examples:

How to Select a Named Range on Another Worksheet in the Same Workbook

To select the named range 'Test' on another worksheet in the same workbook, you can use the following example:

Or, you can activate the worksheet, and then use method 7 above to select the named range:

How to Select a Named Range on a Worksheet in a Different Workbook

To select the named range 'Test' on a worksheet in a different workbook, you can use the following example:

Or, you can activate the worksheet, and then use method 7 above to select the named range:

How to Select a Cell Relative to the Active Cell

To select a cell that is five rows below and four columns to the left of the active cell, you can use the following example:

To select a cell that is two rows above and three columns to the right of the active cell, you can use the following example:

Note

An error will occur if you try to select a cell that is 'off the worksheet.' The first example shown above will return an error if the active cell is in columns A through D, since moving four columns to the left would take the active cell to an invalid cell address.

How to Select a Cell Relative to Another (Not the Active) Cell

To select a cell that is five rows below and four columns to the right of cell C7, you can use either of the following examples:

Excel Vba Example

How to Select a Range of Cells Offset from a Specified Range

To select a range of cells that is the same size as the named range 'Test' but that is shifted four rows down and three columns to the right, you can use the following example:

If the named range is on another (not the active) worksheet, activate that worksheet first, and then select the range using the following example:

How to Select a Specified Range and Resize the Selection

To select the named range 'Database' and then extend the selection by five rows, you can use the following example:

How to Select a Specified Range, Offset It, and Then Resize It

To select a range four rows below and three columns to the right of the named range 'Database' and include two rows and one column more than the named range, you can use the following example:

How to Select the Union of Two or More Specified Ranges

To select the union (that is, the combined area) of the two named ranges 'Test' and 'Sample,' you can use the following example:

Note

that both ranges must be on the same worksheet for this example to work. Note also that the Union method does not work across sheets. For example, this line works fine.

but this line

returns the error message:

Union method of application class failed

How to Select the Intersection of Two or More Specified Ranges

To select the intersection of the two named ranges 'Test' and 'Sample,' you can use the following example:

Note that both ranges must be on the same worksheet for this example to work.

Examples 17-21 in this article refer to the following sample set of data. Each example states the range of cells in the sample data that would be selected.

How to Select the Last Cell of a Column of Contiguous Data

To select the last cell in a contiguous column, use the following example:

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:

When this code is used with the sample table, cell A5 will be selected.

How to Select an Entire Range of Contiguous Cells in a Column

To select a range of contiguous cells in a column, use one of the following examples:

Visual Basic Code Examples In Excel Using

When this code is used with the sample table, cells A1 through A4 will be selected.

How to Select an Entire Range of Non-Contiguous Cells in a Column

To select a range of cells that are non-contiguous, use one of the following examples:

When this code is used with the sample table, it will select cells A1 through A6.

How to Select a Rectangular Range of Cells

In order to select a rectangular range of cells around a cell, use the CurrentRegion method. The range selected by the CurrentRegion method is an area bounded by any combination of blank rows and blank columns. The following is an example of how to use the CurrentRegion method:

This code will select cells A1 through C4. Other examples to select the same range of cells are listed below:

In some instances, you may want to select cells A1 through C6. In this example, the CurrentRegion method will not work because of the blank line on Row 5. The following examples will select all of the cells:

How to Select Multiple Non-Contiguous Columns of Varying Length

To select multiple non-contiguous columns of varying length, use the following sample table and macro example:

When this code is used with the sample table, cells A1:A3 and C1:C6 will be selected.

Notes on the examples

The ActiveSheet property can usually be omitted, because it is implied if a specific sheet is not named. For example, instead of

you can use:

The ActiveWorkbook property can also usually be omitted. Unless a specific workbook is named, the active workbook is implied.

When you use the Application.Goto method, if you want to use two Cells methods within the Range method when the specified range is on another (not the active) worksheet, you must include the Sheets object each time. For example:

For any item in quotation marks (for example, the named range 'Test'), you can also use a variable whose value is a text string. For example, instead of

you can use

Visual Basic Code Examples For Excel Pdf

where the value of myVar is 'Sheet1'.