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

Comprehensive WordTips Archives List

WordTips is a weekly newsletter that provides tips on how to best use Microsoft's word processing software. At ...

Discover More

Preventing Someone from Recreating a Protected Worksheet

When you share a protected workbook with other people, you may not want them to get around the protection by creating a ...

Discover More

Entering a Name in the Header of a Locked Form

When you lock a document as a form, then Word limits what you can do with that document. That includes not being able to ...

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)

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 ...

Discover More

Progression Indicator in a Macro

When your macro is humming along, minding its own business, a user watching the screen may not see any activity and ...

Discover More

Determining the Day of the Month

Want to figure out the day of the month represented by a particular date? You can use the Day function in VBA to get the ...

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. 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 eight minus 1?

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.