Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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

by Allen Wyatt
(last updated December 5, 2016)

7

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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8524) applies to Microsoft Excel 2007, 2010, and 2013. 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

Browsing by Heading

The Object Browser can be a great (albeit underused) way of navigating through your document. One handy way to move about is ...

Discover More

Deleting All Footnotes

Tired of all those footnotes hanging on the bottom of each page in your document? You can get rid of them in one step, as ...

Discover More

Moving Files or Folders

A common operation within Windows is to move files and folders from one location to another. Here are the two major ways that ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Determining the RGB Value of a Color

Excel allows you to fill a cell's background with just about any color you want. If you need to determine the RGB value of ...

Discover More

Understanding the If ... End If Structure

One of the most basic of programming structures is the conditional structure: If ... End If. This tip explains how this ...

Discover More

Worksheet Events

You can create macros that are automatically executed whenever certain events occur within a worksheet. This tip details what ...

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 five less than 5?

2017-05-18 18:51:52

Peter Atherton

Jeff

Wrap the code with Allen's code


ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Select


function lastValue(myRange) {
lastRow = myRange.length
with myrange.SpecialCells(xlCellTypeVisible).Select
for (; myRange[lastRow - 1] == "" && lastRow > 0; lastRow--)
{ /*nothing to do*/ }
return myRange[lastRow - 1];
End with

i Have not test this but giver it a go
};


2017-05-17 12:26:25

Miguel

How could I select a specific number of visible rows?

Thanks a lot.


2015-08-20 13:52:58

jacob

what is the syntax for using this? How do i define what range of cells to select?


2015-02-24 18:09:48

Jeff

So I have a script that I found:
function lastValue(myRange) {
lastRow = myRange.length

for (; myRange[lastRow - 1] == "" && lastRow > 0; lastRow--)
{ /*nothing to do*/ }
return myRange[lastRow - 1];

}; which will grab the last cell in a column, but what do I add to it so that it will only show only filtered data?


2015-01-26 21:25:06

Isaac

I have a workbook with 4 tabs: each tab references each week of the month. So I have FebWeek1, FebWeek2, FebWeek3 & FebWeek4. FebWeek1 is a worksheet with 5 spreadsheets (1 for each day of week1), and each day I work on each spreadsheet: Monday is the first spreadsheet, Tuesday is the 2nd spreadsheet etc... What I want to do is on Monday I want Monday's spreadsheet visible on the entire screen, and Tuesday I want only Tuesday visible on the screen, etc... Any help will be immensely appreciated. Thanks


2014-01-08 09:16:39

Chuck Trese

Hello David,
It is not really appropriate to ask such a question in the comment section of a tip about selecting visible cells.
May I suggest entering "excel vba pivot table hide levels" or something similar in google. This will bring up several really good forums for Excel programming. Understand, however, that these sites have etiquette rules to keep them sane and useful for everyone. Please read each forum's rules before engaging.
Good luck in your search for knowledge!


2014-01-07 06:13:32

David

I have a Pivot Table using 4 fields as breaks/levels for a budget report. The income section actually has 2 levels, while the Expense section has 4 levels.

How can I avoid repeating or turn off the display of the 'extra' levels in the Income section. I only want to display a level if the level above has a different name.

If level 1 and level 2 is INCOME do not display row/subtotals.

If level 1 is EXPENSE and level 2 is FIXED, display the row and subtotals.

Thanks.


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.