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.
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
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:
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.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
It is possible to create macros that send out reports, via e-mail, from within Excel. Frank did this and ran into ...
Discover MoreOne of the most basic of programming structures is the conditional structure: If ... End If. This tip explains how this ...
Discover MoreThere are multiple ways that Excel can create a workbook, and the formatting in each creation method can be different. In ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments