Question posted 2012 · +8 upvotes
I need to find if a cell is visible on the screen.
By visible, I don’t mean hidden. I am specifically trying to find if a cell is currently displayed in the active sheet, or if it is not displayed, ie: it has been scrolled off of the visible active sheet.
I have looked online, and can only find the following code which doesn’t seem to work for me:
Private Sub CommandButton1_Click()
With Worksheets(1).Cells(10, 10)
'MsgBox "Value: " & .Value & ", Top: " & .Top & ", Left: " & .Left
Dim visibleCells As Range
Set visibleCells = Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible)
If Intersect(Worksheets(1).Cells(10, 10), visibleCells) Is Nothing Then
MsgBox "This cell is not visible."
End If
End With
End Sub
Thanks in advance for your help,
Marwan
Accepted answer +11 upvotes
Here’s a function that does what you want:
Function CellIsInVisibleRange(cell As Range)
CellIsInVisibleRange = Not Intersect(ActiveWindow.VisibleRange, cell) Is Nothing
End Function
At least I think it does. I hadn’t been aware of the VisibleRange property until now.
Call it like:
If CellIsInVisibleRange(ActiveSheet.Range("A35")) Then
MsgBox "Cell is visible"
Else
MsgBox "Cell isn't visible"
End If
2 code variants in this answer
- Variant 1 — 3 lines, starts with
Function CellIsInVisibleRange(cell As Range) - Variant 2 — 5 lines, starts with
If CellIsInVisibleRange(ActiveSheet.Range("A35")) Then
Top excel-vba Q&A (6)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- 2 Dimensional array from range +34 (2013)
- Hiding an Excel worksheet with VBA +33 (2009)
excel-vba solutions on this site
.