Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Group/Ungroup a Protected Excel Sheet

Status
Not open for further replies.

SoledWorker

Mechanical
Joined
Aug 20, 2001
Messages
50
Location
US
I'm using Grouped Data in an Excel sheet to allow parts of the spreadsheet to be "collapsed" when they aren't needed. The sheet contains nested groups, and some or all of the data may or may not need to be viewed. This works great, but if I protect the sheet, I can't use the +/- buttons to view/hide data. I wrote a macro that will completely expand or collapse the groups in the sheet, but that really doesn't do what I need: I'd like to protect the cells, but I'd also like to be able to use the +/- buttons to expand/collapse what I want to see. Any ideas on a way around this?...
 
The code that I used to collapse the groups is presented below. This collapses every group, which isn't what is desired but is better than nothing...

Sub Collapse()
Dim blnProtection As Boolean
'Unprotect the sheet, collapse the outline, then re-protect

blnProtection = ActiveSheet.ProtectContents 'true if the sheet is protected

If blnProtection = True Then 'unprotect, collapse and re-protect
ActiveSheet.Unprotect
ActiveSheet.Outline.ShowLevels rowlevels:=1
ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Else 'just collapse
ActiveSheet.Outline.ShowLevels rowlevels:=1
End If

End Sub

This code "remembers" if the sheet was protected, and if so restores it to protected status, or otherwise just collapses the groups. The +/- functionality of excel doesn't appear to be exposed in VB.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top