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

Using Outline Numbering in a Table

Can you put a numbered outline in a table? Yes, you can. But Word is rather prickly when it comes to using the keyboard ...

Discover More

Keep Your Headings in View

Headings on a table are very important when it comes to understanding what is in the table. This tip explains an easy way ...

Discover More

Examining Styles and Macros in a Template

Templates are very powerful with the ability to contain both styles and macros. If you want to see what styles and macros ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Macro, while Running, Stops Excel from Responding

When running a macro, have you ever seen Excel appear to stop responding? This can be frustrating, but there are a couple ...

Discover More

Using InputBox to Get Data

Need your macro to get some input from a user? The standard way to do this is with the InputBox function, described in ...

Discover More

Running a Macro while in Edit Mode

Excel doesn't allow you to run a macro while editing the contents of a cell. The only solution is to get out of Edit ...

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 1 + 1?

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.