简体   繁体   中英

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

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.

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

Or in short:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM