在我的代码中,我包含一个布尔变量,我想在其中分配一个范围的隐藏属性的值。即如果范围是隐藏的,变量应该有值 true,反之亦然。
运行代码时出现“1004”运行时错误 - 无法获取 Range 类的隐藏属性。据此,我假设 Hidden 属性在这种情况下是只写的(如果我错了请纠正我)。
有没有办法确定(在我的代码中,而不是通过观察)范围/单元格是否被隐藏?
我有一个名为“minas”的类,我试图通过这个子类根据某些标准创建一个 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
你可以说一个cell是隐藏的,如果它位于隐藏的行或隐藏的列。
然后如果该范围内的所有单元格都被隐藏,则range是隐藏的:
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
根据 Google 的快速搜索,如果您使用
Range.Find
,如果单元格被隐藏,LookIn:=xlValues
将找不到数据。我在 Cell A6
中使用“测试”对此进行了测试,并隐藏了该行。此代码返回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
获取布尔值(如果单元格隐藏则为真,如果单元格可见则为假)
是否需要判断整列是否被隐藏?无法隐藏单个单元格。 (当然,除非您指的是 HiddenFormula 属性)。如果是这样,下面的代码应该可以工作:
y = x.entirecolumn.Hidden 'does not get the property
让我知道这是否有效