XL: How to Select Ranges Using VB for Apps (Novice Examples)

Last reviewed: February 3, 1998
Article ID: Q148355
The information in this article applies to:
  • Microsoft Excel for Windows, version 5.0, 5.0c
  • Microsoft Excel for the Macintosh, version 5.0
  • Microsoft Excel 98 Macintosh Edition
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows

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.

MORE INFORMATION

Examples of How To Select Cells Manually

In Microsoft Excel, to manually select all the data in a column, select the first cell, hold down the CTRL+SHIFT keys, and then press the DOWN ARROW key.

Likewise, to manually select a row and all columns attached to the row, hold down the CTRL+SHIFT keys, and then press the DOWN ARROW+RIGHT ARROW keys. 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 will 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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

The following Visual Basic code examples will 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("c1", Range("c1").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:

   Sub SelectRangeDown_Discontiguous()
      Range("c1", Range("c16384").End(xlUp)).Select
   End Sub

In Microsoft Excel 97 and Microsoft Excel 98, this macro will be slightly different because there are more rows in a worksheet in this version of Microsoft Excel:

   Sub SelectRangeDown_Discontiguous97()
      Range("c1", Range("c65536").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, try using the following code:

   Sub RangeFromStart()
      Range("d1", Range("d1").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 included in this article show you how to select varying ranges on the active worksheet of your current workbook. For additional information about selecting ranges with Visual Basic for Applications (and for more advanced examples), please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q120198
   TITLE     : XL: How to Select Cells/Ranges Using Visual Basic
               Procedures

REFERENCES

For more information about Select Ranges with the End Property, do the following:

  1. Type "end" (without the quotation marks) on a blank line on a module sheet.

  2. Select the word "end" that you typed in step 1.

  3. Press F1.

  4. In the Context Help dialog box click the item for the "Excel" library, and then click Help (click OK in Microsoft Excel versions 5.0 and 7.0).


Additional query words: 5.00 5.00c 7.00 8.00 ranges select adjacent
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS: 5.0, 7.0, 97; MACINTOSH: 5.0, 98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.