Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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.
by Allen Wyatt
(last updated July 2, 2018)
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:
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:
Similarly, you could select all the visible cells on the entire worksheet by using this line:
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:
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8524) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Selecting Visible Cells in a Macro.
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!
You can use macros to process information in your worksheets. You may want to use that macro to apply the italic ...Discover More
Want to know how much of a time difference there is between your machine and a different machine? This tip provides some ...Discover More
Need to get rid of extraneous spaces before or after the text in a string? VBA provides three different functions you can ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.