Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

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.

like image 993
xiaodai Avatar asked Sep 02 '10 05:09

xiaodai


2 Answers

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
like image 62
Bart Avatar answered Oct 09 '22 18:10

Bart


Or in short:

Sub HideShowRowColumnHeaders()
     ActiveWindow.DisplayHeadings = Not ActiveWindow.DisplayHeadings
End Sub
like image 33
Jesper Avatar answered Oct 09 '22 18:10

Jesper