Microsoft KB Archive/291304

= How to select ranges using Visual Basic for Applications (novice examples) =

Article ID: 291304

Article Last Modified on 1/31/2007

-

APPLIES TO


 * Microsoft Office Excel 2007
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 2000 Standard Edition
 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q291304



SUMMARY
In Microsoft Excel, you can select cells in a range either manually or by using some simple Microsoft Visual Basic for Applications code. This article provides examples you can use to select a range of cells.



Examples of how to select cells manually
To manually select all the data in a column, select the first cell, and press CTRL+SHIFT+DOWN ARROW.

Likewise, to manually select a row and all columns attached to the row, press CTRL+SHIFT+DOWN ARROW+RIGHT ARROW. However, all data must be contiguous (that is, you cannot have blank rows or columns). Also, you can select the current region of data (contiguous data, with no blank rows or columns) by doing the following:
 * 1) On the Edit menu, click Go To.
 * 2) In the Go To dialog box, click Special.
 * 3) In the Go To Special dialog box, click Current region, and then click OK.

You can also select this range by using simple Visual Basic for Applications code.

Note If you try to record this procedure by using the macro recorder, you do not receive the same results.

Examples of how to use Visual Basic code to select cells in a range
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 following Visual Basic code samples show you how to select varying ranges.

If you know the beginning cell (in this example, the beginning cell is cell C1), and you want to select down to the last cell (in the same column) that has data but do not know that address, use the following code: Sub SelectRangeDown Range(&quot;c1&quot;, Range(&quot;c1&quot;).End(xlDown)).Select End Sub Note The SelectRangeDown macro assumes your data is contiguous. Otherwise, if there are blank cells in the column of data you are selecting, this macro may not select all of your cells in the column.

If your data begins in cell C1, but is not contiguous in that column, use the following macro in Microsoft Office Excel 2003 and in earlier versions of Excel: Sub SelectRangeDown_Discontiguous Range(&quot;c1&quot;, Range(&quot;c65536&quot;).End(xlUp)).Select End Sub Because Microsoft Office Excel 2007 supports 1,048,576 rows, use the following macro in Excel 2007: Sub SelectRangeDown_Discontiguous Range(&quot;c1&quot;, Range(&quot;c1048576&quot;).End(xlUp)).Select End Sub If you want to select from the active cell down and all columns to the right (assuming contiguous data in all rows and columns), use the following code: Sub myrangearea Range(ActiveCell, ActiveCell.End(xlDown).End(xlToRight)).Select End Sub If you know the starting cell (in this sample code, the starting cell is D1), and you want to select down the column and to the right, use the following code: Sub RangeFromStart Range(&quot;d1&quot;, Range(&quot;d1&quot;).End(xlDown).End(xlToRight)).Select End Sub To select all data in the current region, use the following code: Sub CurrentArea Selection.CurrentRegion.Select End Sub The examples that are included in this article show you how to select varying ranges on the active worksheet of your current workbook.

For additional information about how to select ranges with Visual Basic for Applications (and for more advanced examples), click the following article number to view the article in the Microsoft Knowledge Base:

291308 How to select cells/ranges using Visual Basic procedures in Excel

Additional query words: OfficeKBHowTo ranges select adjacent inf XL2002 XL2003 XL2007

Keywords: kbdtacode kbhowto kbprogramming KB291304

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.