【问题标题】:table of contents vba- Visible sheets only目录 vba- 仅可见工作表
【发布时间】:2018-02-27 03:59:05
【问题描述】:

我正在尝试创建一个 VBA 代码,它只会为可见工作表创建一个 ToC。我在网上找到了一些 VBA 代码并将其修改为在循环中包含 Visible = True,但是当我运行宏时,隐藏的工作表仍然显示。我已经包含了下面的代码,如果有任何关于调整它以仅显示可见工作表的建议,我将不胜感激。

Sub TableOfContents_Create()

'添加目录工作表以轻松导航到任何选项卡

Dim sht As Worksheet
Dim Content_sht As Worksheet
Dim myArray As Variant
Dim x As Long, y As Long
Dim shtName1 As String, shtName2 As String
Dim ContentName As String

'Inputs
  ContentName = "Contents"

'Optimize Code
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False

'Delete Contents Sheet if it already exists
  On Error Resume Next
    Worksheets("Contents").Activate
  On Error GoTo 0

  If ActiveSheet.Name = ContentName Then
    myAnswer = MsgBox("A worksheet named [" & ContentName & _
      "] has already been created, would you like to replace it?", vbYesNo)

    'Did user select No or Cancel?
      If myAnswer <> vbYes Then GoTo ExitSub

    'Delete old Contents Tab
      Worksheets(ContentName).Delete
  End If

'Create New Contents Sheet
  Worksheets.Add Before:=Worksheets(1)

'Set variable to Contents Sheet
  Set Content_sht = ActiveSheet

'Format Contents Sheet
  With Content_sht
    .Name = ContentName
    .Range("B1") = "Table of Contents"
    .Range("B1").Font.Bold = True
  End With

'Create Array list with sheet names (excluding Contents)
  ReDim myArray(1 To Worksheets.Count - 1)

  For Each sht In ActiveWorkbook.Worksheets
    If sht.Name <> ContentName Then
      myArray(x + 1) = sht.Name
      x = x + 1
    End If
  Next sht

'Alphabetize Sheet Names in Array List
  For x = LBound(myArray) To UBound(myArray)
    For y = x To UBound(myArray)
      If UCase(myArray(y)) < UCase(myArray(x)) Then
        shtName1 = myArray(x)
        shtName2 = myArray(y)
        myArray(x) = shtName2
        myArray(y) = shtName1
      End If
     Next y
  Next x

'Create Table of Contents
  For x = LBound(myArray) To UBound(myArray)
    Set sht = Worksheets(myArray(x))
    sht.Activate
    With Content_sht
      .Hyperlinks.Add .Cells(x + 2, 3), "", _
      SubAddress:="'" & sht.Name & "'!A1", _
      TextToDisplay:=sht.Name
      .Cells(x + 2, 2).Value = x
    End With
  Next x

Content_sht.Activate
Content_sht.Columns(3).EntireColumn.AutoFit


ExitSub:
'Optimize Code
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True

End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    工作表的.Visible 属性有三个选项:

    正如您可能想象的那样,0 转换为 False,1 或 2 转换为 True。如果您尝试将 .Visible 转换为 Boolean 值,这会导致错误。

    因此,我们的想法是仅循环通过 xlSheetVisible 的工作表。简单地检查sht.Visible 可能会导致错误,如果工作表是xlSheetVeryHidden,因为xlSheetVeryHidden 被评估为True

    Public Sub TestMe()
        Dim sht As Worksheet    
        Set sht = Worksheets(1)
        sht.Visible = xlSheetVeryHidden
        Debug.Print CBool(sht.Visible)  'prints true
    End Sub
    

    因此使用:

    If sht.Visible = xlSheetVisible and sht.Name &lt;&gt; ContentName

    【讨论】:

    • 我尝试添加 sht.Visible = xlSheetVisible 并且 ToC 现在只显示标题(“目录”)。还有一个错误-下标超出范围,我很好奇。
    • @smurfimpulse - 尝试找到一种方法将myArray 仅分配给可见工作表。这个ReDim myArray(1 To Worksheets.Count - 1) 也包含隐藏的和非常隐藏的。
    【解决方案2】:

    只循环可见的工作表:

    如果 sht.Name ContentName 和 sht.Visible 那么

    【讨论】:

    • 假设sht.Visible 以您想要的方式转换为布尔值有点危险。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    相关资源
    最近更新 更多