简体   繁体   English

如何确定工作表单元格在VBA中是否可见/显示?

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

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 马尔万

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. 到目前为止我还没有意识到VisibleRange属性。

Call it like: 称之为:

If CellIsInVisibleRange(ActiveSheet.Range("A35")) Then
    MsgBox "Cell is visible"
Else
    MsgBox "Cell isn't visible"
End If

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. @DougGlancy中的函数在大多数情况下都可以使用,但如果Range的行高或列宽设置为零,它将失败。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用VBA计算工作表中的单元格数 - How to count number of cell in a worksheet using vba 以点为单位确定可见的工作表区域大小 - Determine Visible Worksheet Area Size in Points 如何从 Excel VBA 代码中的另一个工作表评估单元格 - How to assess Cell from another worksheet in Excel VBA code 如何使用VBA将修改后的xml文件保存到工作表单元格 - how to save modified xml file to a worksheet cell using vba 如何为工作簿 VBA 中的每个工作表将单元格值定义为同一列 - How to define cell values to the same column for each worksheet in the workbook VBA 如何按照工作表中的格式在 VBA 中检索 Excel 单元格值? - How do I retrieve an Excel cell value in VBA as formatted in the WorkSheet? 如何使用 VBA 根据活动工作表中的单元格自动填充 excel 表单? - How to autofill the excel form based on the cell in the active worksheet using VBA? 如何在 VBA 中检索 Excel 工作表单元格的 A1 样式引用 - How to retrieve the A1-style reference of an Excel worksheet cell in VBA Excel VBA如何根据不同工作表中单元格的值删除行 - Excel VBA How to delete rows based on the value of a cell in a different worksheet 如何获取当前VBA函数返回的工作表和单元格? - How to get worksheet and cell that the current VBA function is returning to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM