如何确定 Excel 范围是否被隐藏?

How do I determine if an Excel range is hidden?

在我的代码中,我包含了一个布尔变量,我想在其中分配一个范围的隐藏值 属性。即如果范围是隐藏的,变量应该有值 true,反之亦然。

虽然 运行 代码出现“1004”运行 时间错误 - 无法获取范围 class 的隐藏 属性。由此我假设 Hidden 属性 在这种情况下是只写的(如果我错了请纠正我)。

有没有办法确定(在我的代码中,不是通过观察)range/cell 是否被隐藏?

我有一个名为 "minas" 的 class,我试图通过这个子程序根据某些条件创建一个 minas 集合。

Public mines As Collection
Sub existing_months()
    Set mines = New Collection
    Dim min As minas
    Dim str As String
    Dim x As Range
    Dim y As Boolean
    For i = 1 To 12
        Set min = New minas
        Set x = Range("A1:A500").Find(i, LookIn:=xlValues, LookAt:=xlWhole)
        If x Is Nothing Then GoTo next_iteration:
        y = x.Hidden 'does not get the property
        Call min.initialize(x, y)
        str = min.minas & "/" & min.etos
        mines.Add min, str
        Debug.Print min.ref_range.Address & " " & min.end_cell
next_iteration:
    Next
    Set min = Nothing
End Sub

是否需要判断整列是否被隐藏?无法隐藏单个单元格。 (当然,除非您指的是 HiddenFormula 属性)。如果是这样,下面的代码应该可以工作:

y = x.entirecolumn.Hidden 'does not get the property

让我知道这是否有效

根据快速 Google 搜索,如果您使用 LookIn:=xlValues,如果单元格被隐藏,Range.Find 将找不到数据。我在 Cell A6 中用 "Test" 测试了这个并隐藏了该行。此代码返回 Nothing:

Sub TestIt()
    Dim x As Range
    Set x = Range("A1:A7").Find("Test", , xlValues, xlWhole)
    If x Is Nothing Then
        MsgBox "Nothing"
    Else
        If x.EntireRow.Hidden = True Then
            MsgBox x.Address & " is Hidden"
        Else
            MsgBox x.Address & " is Visible"
        End If
    End If
End Sub

您需要使用 LookIn:=xlFormulas:

Sub TestIt()
    Dim x As Range
    Set x = Range("A1:A7").Find("Test", , xlFormulas, xlWhole)
    If x Is Nothing Then
        MsgBox "Nothing"
    Else
        If x.EntireRow.Hidden = True Then
            MsgBox x.Address & " is Hidden"
        Else
            MsgBox x.Address & " is Visible"
        End If
    End If
End Sub

那么您可以使用:

y = x.EntireRow.Hidden

y = x.EntireColumn.Hidden

获取布尔值(如果单元格隐藏则为真,如果单元格可见则为假)

如果单元格位于隐藏行或隐藏列,则可以说它是隐藏的。
然后是范围 如果该范围内的所有单元格都隐藏则隐藏:

Public Function IsHidden(rIn As Range) As Boolean
    Dim r As Range
    IsHidden = True
    For Each r In rIn
        If Not r.EntireRow.Hidden Then
            If Not r.EntireColumn.Hidden Then
                IsHidden = False
                Exit Function
            End If
        End If
    Next r
End Function