如何编写 VBA 代码来隐藏 Excel 中的所有列和行标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3624110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:44:59  来源:igfitidea点击:

How to write VBA code to hide all the column and row headings in Excel?

excelvba

提问by xiaodai

Private Sub hideHeadings()
  Dim obj As Window
  For Each obj In Application.Windows
    obj.DisplayHeadings = False
  Next obj

End Sub

The above is my attempt, as when I recorded code to do it it goes ActiveWindow.DisplayHeadings = false. But I must be missing something. Please help thanks in advance.

以上是我的尝试,因为当我记录代码时,它会变为 ActiveWindow.DisplayHeadings = false。但我一定错过了一些东西。请帮助提前致谢。

回答by Bart

I think there is nothing you can do with that except iterating on each worksheet. I succeed with this code

我认为除了在每个工作表上迭代之外,您无能为力。我成功使用此代码

Private Sub hideHeadings()
  Dim wrkbk As Workbook
  Dim wrksh As Worksheet
  Dim prev As Window

  Set prev = ActiveWindow

  For Each wrkbk In Workbooks
    For Each wrksh In wrkbk.Worksheets
        wrksh.Activate
        ActiveWindow.DisplayHeadings = False
    Next wrksh
  Next wrkbk

  prev.Activate

End Sub

回答by Jesper

Or in short:

或者简而言之:

Sub HideShowRowColumnHeaders()
     ActiveWindow.DisplayHeadings = Not ActiveWindow.DisplayHeadings
End Sub

回答by jpumar

The code below toggles between hiding and showing the headings depending on what the their current status is. It could be more useful as sometimes we don't want to hide or show the headings for all the sheets in the workbook but the one we are currently working with.

下面的代码根据标题的当前状态在隐藏和显示标题之间切换。它可能更有用,因为有时我们不想隐藏或显示工作簿中所有工作表的标题,而是我们当前正在使用的工作表。

Sub HideShowRowColumnHeaders()

    Dim StatusOfHeadings As Boolean

    StatusOfHeadings = ActiveWindow.DisplayHeadings

    If StatusOfHeadings Then
            ActiveWindow.DisplayHeadings = False
        Else
        ActiveWindow.DisplayHeadings = True
    End If

End Sub