Excel VBA - Selecting multiple sheets

December 2016




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 :

This document entitled « Excel VBA - Selecting multiple sheets » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.