Written by Allen Wyatt (last updated July 11, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Selecting Visible Cells in a Macro.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
If you are getting an error when you try to create an event handler, it could be related to a long-known bug in Excel. ...
Discover MoreTracking down memory errors in a macro can be frustrating. The error message is inherently vague and correcting any ...
Discover MoreDo you need to create a number of words or phrases where you only alter a few letters in each one? If the alterations ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2019-12-04 07:36:06
Hi KP
Does this work?
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
' MsgBox Sh.Name & ":" & Target.Address
MsgBox Target.Address
End Sub
2019-12-03 23:34:12
KP
i have a requirement in my macros to identify the last visible cell on screen. For example, my codes opens a data sheet, finds a value, say in "B14" of this data sheet and places this cell as first cell to the viewer. (like visible area on screen is "B14:C34" )... Now i want the vba to identify the last visible cell, in this example "C34". There may or may not be any data in the last cell. Can i get the starting cell as B14 & last cell as C34 when a Worksheet_SelectionChange(ByVal Target As Excel.Range) is triggered ?
Thanks in advance
Krishna
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 © 2023 Sharon Parq Associates, Inc.
Comments