Excel VBA - Selecting multiple sheets

May 2017




Issue


I have a certain VBA code that I use for group/ungroup on protected sheet. It works just fine but only on one sheet. How can I modify it to work on two?

Private Sub Workbook_open() 
With Sheet1 
.Protect Password:="secret", userinterfaceonly:=True 
.EnableOutlining = True 
End With 
End Sub 



I don't know how to add more than one sheet. Can somebody help me?

Solution


Try this:

Private Sub Workbook_open() 
With sheets("Sheet1 name") 
.Protect Password:="secret", userinterfaceonly:=True 
.EnableOutlining = True 
End With 
With sheets("Sheet2 name") 
.Protect Password:="secret", userinterfaceonly:=True 
.EnableOutlining = True 
End With 
End Sub

Note


Thanks to Excelguru for this tip on the forum.

Related


Published by aakai1056. Latest update on March 3, 2010 at 09:49 AM by aakai1056.
This document, titled "Excel VBA - Selecting multiple sheets," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).