XL: Macro to Select all Data on a Worksheet

ID: Q89949

The information in this article applies to:

SUMMARY

In Microsoft Excel, you can select all the data on your worksheet by pressing the key combination CTRL+SHIFT+END (CONTROL+SHIFT+END on Macintosh). You can also simulate these keystrokes in the macro described in this article.

MORE INFORMATION

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

Sample Visual Basic Procedure

The following procedure selects data from the currently selected cell, to the end of the data on your worksheet.

   Sub Select_All()
      Range(Selection, Selection.SpecialCells(xlLastCell)).Select
   End Sub

You can easily modify the procedure to select from a specific cell by adding the following statement to the beginning of the procedure:

   Range("B2").Select

where B2 is the reference of the specific cell.

Microsoft Excel version 4.0 Macro

The following macro selects data from the currently selected cell, to the end of the data on your worksheet. You can easily modify the macro to select from a specific cell by changing the SELECT() function in cell A2 to select the starting cell you want.

For example, to select from the first cell (R1C1) on your worksheet, change the SELECT() function in A2 to read: =SELECT("R1C1").

To create and run this macro:

1. Enter the following macro on a macro sheet:

      A1: SelectAll
      A2: =SELECT("RC")
      A3: =GET.CELL(1)
      A4: =SELECT.LAST.CELL()
      A5: =GET.CELL(1)
      A6: =SELECT(REFTEXT(INDIRECT(A3))&":"&REFTEXT(INDIRECT(A5)))
      A7: =RETURN()

2. Select cell A1 and choose Define Name from the Formula menu.

3. The name SelectAll should show up in the Name box in the Define

   Name dialog box.

   If not, you already have a name defined as SelectAll, so choose a
   different name for this macro and enter it in the Name box.

4. In the Define Name dialog box, select the Command option.

5. If you like, you can enter a shortcut key for this macro by

   selecting the Key: Ctrl+ box and typing the character you choose.

6. In the Define Name dialog box, choose the OK button.

7. To run your macro, choose Run from the Macro menu and select your

   macro name from the list, or, if you defined a shortcut key, press
   CTRL+<your shortcut key> (use CONTROL on the Macintosh).

REFERENCES

"Microsoft Excel User's Guide 2," version 4.0, pages 231-238

"Microsoft Excel Function Reference," version 4.0, pages 191-194, 238- 239, 349-350, 378-379

"Microsoft Excel User's Guide," version 3.0, pages 595-600

"Microsoft Excel Function Reference," version 3.0, pages 103-105, 129, 192-193, 209-210

For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications

Additional query words: selection range 3.0 3.00 4.0 4.00 5.00 7.00
Keywords          : kbcode kbprg PgmOthr 
Version           : WINDOWS:3.0,4.0,5.0,7.0; MACINTOSH:3.0,4.0,98
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowto

Last Reviewed: May 17, 1999