使用VBA在Excel中查找隐藏的工作表(和隐藏的单元格)

时间:2011-09-30 23:19:26

标签: excel vba

有没有办法确定Excel工作簿是否使用VBA隐藏了工作表和/或隐藏的单元格?谢谢!

4 个答案:

答案 0 :(得分:8)

您可以遍历工作表,列和行,检查worksheet.visible和range.hidden属性。下面是一些快速而脏的代码,它们会将任何隐藏的元素输出到即时窗口。

Sub FindHidden()
    Dim wks As Worksheet
    Dim rng As Range

    For Each wks In ThisWorkbook.Worksheets
        If wks.Visible = xlSheetHidden Then
            Debug.Print "Worksheet: " & wks.Name & " is hidden."
        ElseIf wks.Visible = xlSheetVeryHidden Then
            Debug.Print "Worksheet: " & wks.Name & " is very hidden."
        End If

        For Each rng In wks.UsedRange.Rows
            If rng.Hidden = True Then
                Debug.Print "Worksheet: " & wks.Name & " Hidden Row: " & rng.Row
            End If
        Next rng

        For Each rng In wks.UsedRange.Columns
            If rng.Hidden = True Then
                Debug.Print "Worksheet: " & wks.Name & " Hidden Column: " & Left(Replace(rng.Address, "$", ""), 1)
            End If
        Next rng
    Next wks
End Sub

答案 1 :(得分:2)

我将以下过程保存在Personal.xls文件中,并在快速访问工具栏上有一个按钮来运行它。它会在弹出的对话框中显示所有隐藏的工作表和非常隐藏的工作表,还可以取消隐藏单个工作表或所有隐藏工作表,所有隐藏工作表或所有隐藏工作表。

这不显示隐藏的单元格/行/列,但对查找和取消隐藏工作表非常有用。我使用上面提到的Dave的Mappit Addin进行更详细的分析。

代码如下:

    Sub UnHideStuff()

'----------------------------------------------------------------------------
' UnHideStuff Macro
' Written by ProdOps
' 13-Feb-2010
'
' Provides an input dialog box that displays the names of all Hidden and all
' VeryHidden worksheets in the workbook and allows the user to enter the
' name of the worksheet they want to unhide.
' *   will unhide all Veryhidden sheets
' **  will unhide all Hidden sheets.
' *** will unhide all worksheets in the workbook
'
'----------------------------------------------------------------------------

 Dim Message As String
 Dim Title As String
 Dim Default As String
 Dim myValue As String
 Dim myList As String
 Dim Sheetnum As Long

'Build a list of VeryHidden Sheets
 myList = "'INVISIBLE WORKSHEET NAMES(*)':"
 For Sheetnum = 1 To Sheets.Count
   If Sheets(Sheetnum).Visible = 2 Then
     myList = myList & vbCrLf & "   " & Sheets(Sheetnum).Name
   End If
 Next Sheetnum
 If myList = "'INVISIBLE WORKSHEET NAMES(*)':" Then
   myList = myList & vbCrLf & "   No Invisible Sheets in This Workbook"
 End If

 'Build a list of Hidden Sheets
 myList = myList & vbCrLf & vbCrLf & "'HIDDEN WORKSHEET NAMES(**)':"
 For Sheetnum = 1 To Sheets.Count
   If Sheets(Sheetnum).Visible = 0 Then
     myList = myList & vbCrLf & "   " & Sheets(Sheetnum).Name
   End If
 Next Sheetnum
 If Right(myList, 11) = "NAMES(**)':" Then
   myList = myList & vbCrLf & "   No Hidden Sheets in This Workbook"
 End If

 'Build the Textbox Message & Title
 Message = "Enter the 'Name' of the WorkSheet to Unhide" & vbCrLf
 Message = Message & "Or * - All Invisible, ** - All Hidden, *** - All" & vbCrLf & vbCrLf
 Message = Message & myList
 Title = "Unhide Hidden Worksheets"
 Default = ""

 'Display the Message Box and retrive the user's input
 myValue = InputBox(Message, Title, Default)

 'Test the value entered by the user
 If myValue = "" Then Exit Sub  'User pressed CANCEL

 If myValue = "*" Then   'User wants all the VeryHidden sheets displayed
   For Sheetnum = 1 To Sheets.Count
    If Sheets(Sheetnum).Visible = 2 Then Sheets(Sheetnum).Visible = True
   Next Sheetnum
   GoTo NormalExit
 End If

 If myValue = "**" Then   'User wants all the Normal Hidden sheets displayed
   For Sheetnum = 1 To Sheets.Count
    If Sheets(Sheetnum).Visible = 0 Then Sheets(Sheetnum).Visible = True
   Next Sheetnum
   GoTo NormalExit
 End If

 If myValue = "***" Then   'User wants all worksheets displayed
   For Sheetnum = 1 To Sheets.Count
    Sheets(Sheetnum).Visible = True
   Next Sheetnum
   GoTo NormalExit
 End If

 On Error GoTo ErrorTrap
 Sheets(myValue).Visible = xlSheetVisible
 Sheets(myValue).Select
 Range("A1").Select

NormalExit:
 Exit Sub

ErrorTrap:
 If Err = 9 Then
   MsgBox "Either the Worksheet Does Not Exist or " & vbCrLf & "the Worksheet Name was Misspelled", vbCritical, "Worksheet Not Found"
   Err.Clear
   Call UnHideStuff
 End If

End Sub

答案 2 :(得分:0)

另一种选择是我的(免费)Mappit! addin available here突出显示

  1. 每张测试纸上的隐藏区域(见下面的粉色阴影区域),
  2. 还会生成一个相互链接的工作表摘要,其中列出了工作表如何与离散公式连接(无论它们是可见的,隐藏的还是非常隐藏的)
  3. 您的问题促使我查看将图纸链接输出更新为颜色突出显示哪些图纸被隐藏或非常隐藏。

    [更新:MappitV1.11更新如下,提供有关工作表可见性的信息。现在进一步更新为MappitV1.11a,因为隐藏的空白表未在摘要表上标记]

    enter image description here enter image description here enter image description here

答案 3 :(得分:0)

这是一个非常类似于Banjoe的方法,它将返回有多少隐藏的工作表,列和行的数量(假设您不需要关于哪些行的信息而只需要报告)。< / p>

  • 请注意,对行/列使用'UsedRange'意味着计数不包含不包含任何数据的行/列(但结果会更快)。

以下是代码:

Sub HiddenReport()

Application.ScreenUpdating = False
Dim wks As Worksheet
Dim rng As Range
Dim sCount As Long, rCount As Long, cCount As Long

For Each wks In ThisWorkbook.Worksheets
    If wks.Visible = xlSheetHidden Then sCount = sCount + 1
    If wks.Visible = xlSheetVeryHidden Then sCount = sCount + 1

    For Each rng In wks.Rows ' or wks.UsedRange.Rows
        If rng.Hidden = True Then rCount = rCount + 1
    Next

    For Each rng In wks.Columns ' or wks.UsedRange.Columns
        If rng.Hidden = True Then cCount = cCount + 1
    Next
Next

Application.ScreenUpdating = True
MsgBox sCount & " hidden sheets found." & vbLf & _
       rCount & " hidden rows found." & vbLf & _
       cCount & " hidden columns found."

End Sub

请注意,您还可以使用Excel中的“检查文档”功能查看文档是否隐藏了工作表/行/列。