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

在我的代码中,我包含一个布尔variables,我想在其中指定一个范围的隐藏属性的值。 即如果范围是隐藏的,则该variables应该具有真值,反之亦然。

运行代码时,我得到一个'1004'运行时错误 – 无法获取Range类的Hidden属性。 通过这个我假设在这种情况下隐藏的属性是只写(纠正我,如果我错了)。

有没有办法确定(在我的代码,而不是通过观察)范围/细胞是否隐藏?

我有一个名为“minas”的类,在这个子类中,我试图根据一些条件创build一个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 

根据Google的快速search,如果使用LookIn:=xlValues如果单元格被隐藏,则Range.Find将无法find数据。 我在单元格A6中用“testing”testing了这个并隐藏了这一行。 此代码返回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 

你需要确定整个列是否隐藏? 单个单元格不能隐藏。 (当然,除非你指的是HiddenFormula属性)。 如果是这样,下面的代码应该工作:

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

让我知道这个是否奏效