How to determine if a worksheet Cell is Visible/Displayed in VBA?

后端 未结 2 1499
鱼传尺愫
鱼传尺愫 2020-12-19 01:40

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

2条回答
  •  一整个雨季
    2020-12-19 02:15

    The function from @DougGlancy will work in most instances but it fails if the Range has a row height or column width set to zero. This function adds logic to deal with that plus some error handling.

    Function Range_IsVisibleInWindow(ByVal target As Excel.Range) As Boolean
    ' Returns TRUE if any cell in TARGET (Range) is visible in the Excel window.
    '
    '   Visible means (1) not hidden, (2) does not have row height or column width of
    '   zero, (3) the view is scrolled so that the Range can be seen by the user at
    '   that moment.
    '
    '   A partially visible cell will also return TRUE.
    
        If target Is Nothing Then
            ' Parameter is invalid.  Raise error.
            Err.Raise 3672, _
                      "Range_IsVisibleInWindow()", _
                      "Invalid parameter in procedure 'Range_IsVisible'."
    
        Else
            ' Parameter is valid.  Check if the Range is visible.
            Dim visibleWinLarge As Excel.Range
            Dim visibleWinActual As Excel.Range
    
            On Error Resume Next
            Set visibleWinLarge = Excel.ActiveWindow.VisibleRange ' active window range -INCLUDING- areas with zero column width/height
            Set visibleWinActual = visibleWinLarge.SpecialCells(xlCellTypeVisible) ' active window range -EXCLUDING- areas with zero column width/height
            Range_IsVisibleInWindow = Not Intersect(target, visibleWinActual) Is Nothing ' returns TRUE if at least one cell in TARGET is currently visible on screen
            On Error GoTo 0
    
        End If
    End Function
    

提交回复
热议问题