Article ID: 291308 - Last Review: January 31, 2007 - Revision: 6.1 How to select cells/ranges by using Visual Basic procedures in ExcelThis article was previously published under Q291308 On This PageMORE INFORMATIONMicrosoft 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.
Method Arguments
------------------------------------------
Activate none
Cells rowIndex, columnIndex
Application.Goto reference, scroll
Offset rowOffset, columnOffset
Range cell1
cell1, cell2
Resize rowSize, columnSize
Select none
Sheets index (or sheetName)
Workbooks index (or bookName)
End direction
CurrentRegion none
Property Use --------------------------------------------------------------------- ActiveSheet to specify the active sheet ActiveWorkbook to specify the active workbook Columns.Count to count the number of columns in the specified item Rows.Count to count the number of rows in the specified item Selection to refer to the currently selected range 1: How to Select a Cell on the Active WorksheetTo select cell D5 on the active worksheet, you can use either of the following examples:
-or-
2: How to Select a Cell on Another Worksheet in the Same WorkbookTo select cell E6 on another worksheet in the same workbook, you can use either of the following examples:-or- 3: How to Select a Cell on a Worksheet in a Different WorkbookTo select cell F7 on a worksheet in a different workbook, you can use either of the following examples:
-or-
4: How to Select a Range of Cells on the Active WorksheetTo select the range C2:D10 on the active worksheet, you can use any of the following examples:5: How to Select a Range of Cells on Another Worksheet in the Same WorkbookTo select the range D3:E11 on another worksheet in the same workbook, you can use either of the following examples:6: How to Select a Range of Cells on a Worksheet in a Different WorkbookTo select the range E4:F12 on a worksheet in a different workbook, you can use either of the following examples:7: How to Select a Named Range on the Active WorksheetTo select the named range "Test" on the active worksheet, you can use either of the following examples:8: How to Select a Named Range on Another Worksheet in the Same WorkbookTo select the named range "Test" on another worksheet in the same workbook, you can use the following example:9: How to Select a Named Range on a Worksheet in a Different WorkbookTo select the named range "Test" on a worksheet in a different workbook, you can use the following example:10: How to Select a Cell Relative to the Active CellTo select a cell that is five rows below and four columns to the left of the active cell, you can use the following example:11: How to Select a Cell Relative to Another (Not the Active) CellTo 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:12: How to Select a Range of Cells Offset from a Specified RangeTo 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:13: How to Select a Specified Range and Resize the SelectionTo select the named range "Database" and then extend the selection by five rows, you can use the following example:14: How to Select a Specified Range, Offset It, and Then Resize ItTo 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:15: How to Select the Union of Two or More Specified RangesTo select the union (that is, the combined area) of the two named ranges "Test" and "Sample," you can use the following example: Union method of
application class failed 16: How to Select the Intersection of Two or More Specified RangesTo select the intersection of the two named ranges "Test" and "Sample," you can use the following example: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. A1: Name B1: Sales C1: Quantity A2: a B2: $10 C2: 5 A3: b B3: C3: 10 A4: c B4: $10 C4: 5 A5: B5: C5: A6: Total B6: $20 C6: 20 17: How to Select the Last Cell of a Column of Contiguous DataTo select the last cell in a contiguous column, use the following example:18: How to Select the Blank Cell at Bottom of a Column of Contiguous DataTo select the cell below a range of contiguous cells, use the following example:19: How to Select an Entire Range of Contiguous Cells in a ColumnTo select a range of contiguous cells in a column, use one of the following examples:-or- 20: How to Select an Entire Range of Non-Contiguous Cells in a ColumnTo select a range of cells that are non-contiguous, use one of the following examples:-or- 21: How to Select a Rectangular Range of CellsIn 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:-or-
-or-
22. How to Select Multiple Non-Contiguous Columns of Varying LengthTo select multiple non-contiguous columns of varying length, use the following sample table and macro example:A1: 1 B1: 1 C1: 1 D1: 1 A2: 2 B2: 2 C2: 2 D2: 2 A3: 3 B3: 3 C3: 3 D3: 3 A4: B4: 4 C4: 4 D4: 4 A5: B5: 5 C5: 5 D5: A6: B6: C6: 6 D6: NOTES ON THE EXAMPLES
REFERENCESFor more information about how to use the sample code in this article, click the following article number to view the article in the Microsoft Knowledge Base: 290140
(http://support.microsoft.com/kb/290140/
)
How to run the sample code for the Office XP programs from Knowledge Base articles
| Article Translations
|
Back to the top
