+ Reply to Thread
Results 1 to 2 of 2

Expand and Collapse Grouping Code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Expand and Collapse Grouping Code

    Hello,

    I have a macro setup to protect and unprotect all sheets in a workbook. I have half of this working... This portion locks all of the sheets with a specified password, and they must know the password to do so. This locks all sheets and collapses the groupings as it should.

    Sub Button1_Click()
    
        Dim objSheet As Worksheet
        Dim pw As String
        
        pw = InputBox("Please Enter Password", "Password")
        If pw = "XXX" Then
        
        'Protecting all worksheets with password
        For Each objSheet In Worksheets
            
            objSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
            
            If objSheet.ProtectContents = False Then objSheet.Protect DrawingObjects:=True, _
            Contents:=True, Scenarios:=True, AllowFiltering:=True, Password:="XXX"
        
        Next objSheet
        Workbooks("Macros.xlsm").Close (SaveChanges = No)
        MsgBox ("Complete")
        Else: MsgBox ("Invalid Password")
    
        End If
    
    End Sub
    However, This code which is for unlocking the sheets, I want it to expand all groupings.... it is not doing so as I thought it would.

    Sub Button2_Click()
    
        Dim objSheet As Worksheet
        Dim pw As String
        
        pw = InputBox("Please Enter Password", "Password")
        If pw = "XXX" Then
        'UnProtecting all worksheets with password
        
        For Each objSheet In Worksheets
            If objSheet.ProtectContents = True Then objSheet.Unprotect pw
            objSheet.Outline.ShowLevels 8
        Next objSheet
        Workbooks("Macros.xlsm").Close (SaveChanges = No)
        Else: MsgBox ("Invalid Password")
        End If
           
        MsgBox ("Complete")
        
    End Sub

    Where am I going wrong with the ungrouping? I would think it should work much as the grouping portion does correctly.
    Last edited by Dulanic; 04-15-2010 at 01:04 PM.

  2. #2
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: Expand and Collapse Grouping Code

    Well I feel stupid....All Set!

    Sub Button2_Click()
    
        Dim objSheet As Worksheet
        Dim pw As String
        
        pw = InputBox("Please Enter Password", "Password")
        If pw = "XXX" Then
        'UnProtecting all worksheets with password
        
        For Each objSheet In Worksheets
            If objSheet.ProtectContents = True Then objSheet.Unprotect pw
            objSheet.Outline.ShowLevels rowLevels:=8, columnLevels:=8
        Next objSheet
        Workbooks("Macros.xlsm").Close (SaveChanges = No)
        Sheet4.Activate
        Else: MsgBox ("Invalid Password")
        End If
           
        MsgBox ("Complete")
        
    End Sub
    Sub Button1_Click()
    
        Dim objSheet As Worksheet
        Dim pw As String
        
        pw = InputBox("Please Enter Password", "Password")
        If pw = "XXX" Then
        
        'Protecting all worksheets with password
        For Each objSheet In Worksheets
            
            objSheet.Outline.ShowLevels rowLevels:=1, columnLevels:=1
            
            If objSheet.ProtectContents = False Then objSheet.Protect DrawingObjects:=True, _
            Contents:=True, Scenarios:=True, AllowFiltering:=True, Password:="XXX"
        
        Next objSheet
        Workbooks("Macros.xlsm").Close (SaveChanges = No)
        MsgBox ("Complete")
        Else: MsgBox ("Invalid Password")
    
        End If
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1