Autoexec Macro

  • Thread starter Thread starter Briank
  • Start date Start date
B

Briank

I would like to create a macro that, upon opening the
file, looks at each cell in Row two and if that cell has
the term "hide" then the macro will hide that column and
for every column in the range.

Column 1 2 3 4 5 6

row 1 SF LA NY Chi Bos Cle
row 2 hide view hide view view view

Any suggestion on how to code this?

Thank you.
 
Brian,

Private Sub Workbook_Open()
Dim i As Long

For i = 1 To Cells(2, Columns.Count).End(xlToLeft).Column
If Cells(2, i).Value = "hide" Then
Cells(2, i).EntireColumn.Hidden = True
End If
Next

End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Thanks for the input. The code works great.
However, when I four worksheets in my file and the code
only works for the first worksheet. Even though I have
copied the code into four objects I still get only one
worksheet with the correctly run code. Any thoughts?
 
This will work for all worksheets in the workbook
The event will run when you open the workbook

Private Sub Workbook_Open()
Dim Cnum As Integer
Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
For Cnum = 1 To 256
If sh.Cells(2, Cnum).Value = "hide" Then sh.Columns(Cnum).Hidden = True
Next
Next
End Sub

Right click on the Excel icon next to File in the menubar
And choose View code

You are now in the Thisworkbook module
Paste the Event in this place
Alt-Q to go back to Excel
Save and close the file
 
Back
Top