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

Word Freezes when Updating Styles

If you have problems with Word freezing at times, it can be very frustrating. Here are a couple of things you can check ...

Discover More

Copying Data between Worksheets Using a Macro

Macros can be used for all sorts of data processing needs. One need that is fairly common is the need to copy data from ...

Discover More

Monday's Date on Friday's Report

Automatically putting today's date at the top of a report is easy. Putting a different date, automatically, can be more ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Removing All Macros

Macros are stored as part of a workbook so that they are always available when you have the workbook open. If you want to ...

Discover More

Item Not Available in Library

When sharing workbooks with others, you may find that the macros in those workbooks may not work as you expect. This tip ...

Discover More

Setting Column Width in a Macro

Does your macro need to change the width of some columns in a worksheet? Here's how to do it.

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 four minus 0?

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.