Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Selecting Visible Cells in a Macro.

Selecting Visible Cells in a Macro

Written by Allen Wyatt (last updated November 11, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


2

Karthi notes that he often needs to use Go To Special to select just the visible cells in a selection. This makes him wonder if there is a way that such cells can be selected in a macro.

There are numerous ways that just the visible cells can be selected without a macro, but those won't be gone into here. The assumption is that you want to select the visible cells as part of a larger macro you may be creating. For instance, you might need to select the visible cells before doing some sort of formatting or before you process the cells in some other way.

To select just the visible cells from a range of selected cells, you can use the following line of code:

Selection.SpecialCells(xlCellTypeVisible).Select

If you need to work on some other initial range of cells before selecting the visible subset of those cells, all you need to do is change the "Selection" portion of the line. For instance, you could select the visible cells in the used range of the worksheet by using this line:

ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Select

Similarly, you could select all the visible cells on the entire worksheet by using this line:

Cells.SpecialCells(xlCellTypeVisible).Select

Keep in mind that the techniques described so far select all the cells that are visible, even if they are off-screen. In other words, the techniques select any non-hidden cells in the worksheet. If you truly want to select only those non-hidden cells that are visible on the screen at the current time, then you can use a different technique:

Intersect(MyRange, ActiveWindow.VisibleRange).SpecialCells(xlCellTypeVisible)

The code starts by selecting only those cells where a given range (in this case "MyRange") intersects with the visible range of cells in the active window. These cells are further winnowed down by using the SpecialCells collection to make sure that only non-hidden cells are used.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8524) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Selecting Visible Cells in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Adding Line Numbers

Many types of documents, such as legal documents, require the use of line numbers to make it easier to identify specific ...

Discover More

Requiring Input

If you distribute a workbook that is used by others for data entry, you may want a way to make sure they fill in certain ...

Discover More

Plain Text Pasting as the Default

Pasting 'plain text' into a document is one of the most common ways of pasting information. Wouldn't it be great if this ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Inserting the Current Time with Seconds

If you need to insert the current time, with seconds, then you'll need the macro discussed in this tip. It's easy to use ...

Discover More

Selecting the First Cell In a Row

When creating macros, you'll often have a need to select different cells in the worksheet. Here's how to select the first ...

Discover More

Bypassing the BeforeClose Event

Hold down the Shift key as you open a workbook, and Excel bypasses any "startup macros" that may be in the workbook. If ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

2023-11-23 12:15:04

J. Woolley

For more about the Range.SpecialCells method, see my recent comment here: https://excelribbon.tips.net/T012552_Skipping_Hidden_Rows_in_a_Macro.html


2023-11-11 10:38:39

J. Woolley

This Tip is of course correct when a Macro is understood to be a Sub procedure. However, the Range.SpecialCells method is unreliable and should be avoided (directly or indirectly) in a Function procedure intended for use in a cell formula (UDF). The reason is best explained in the last paragraph of the following reference:
https://support.microsoft.com/en-us/topic/description-of-limitations-of-custom-functions-in-excel-f2f0ce5d-8ea5-6ce7-fddc-79d36192b7a1
If that reference fails, search for the following:
Description of limitations of custom functions in Excel


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.