Results 1 to 10 of 10

Automatically Delete Unused Sheets in Protected Wkbk with Protected Sheets?

Threaded View

leaning Automatically Delete Unused... 07-01-2010, 10:08 PM
davesexcel Re: Automatically Delete... 07-01-2010, 11:21 PM
foxguy Re: Automatically Delete... 07-02-2010, 12:15 PM
leaning Re: Automatically Delete... 07-02-2010, 03:25 PM
foxguy Re: Automatically Delete... 07-02-2010, 03:37 PM
leaning Re: Automatically Delete... 07-02-2010, 04:22 PM
leaning Re: Automatically Delete... 07-02-2010, 04:38 PM
foxguy Re: Automatically Delete... 07-02-2010, 04:42 PM
leaning Re: Automatically Delete... 07-06-2010, 02:04 PM
foxguy Re: Automatically Delete... 07-06-2010, 08:49 PM
  1. #1
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Question Automatically Delete Unused Sheets in Protected Wkbk with Protected Sheets?

    Hello!

    I thought this was "Solved", but then I stuck the code I got from this forum into our workbook which has protected sheets, protected workbook, and Save disabled. I have the syntax or order or something messed up because I am getting Run Time Error 424: Object Required.

    Here's the bad code which tries to unprotect the sheets and I would think you have to unprotect the workbook if it is going to delete sheets, but it isn't there yet:


    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If SaveAsUI = False Then
    Cancel = True
    MsgBox "You cannot save this workbook using ""Save"". Use ""Save As""."
    GoTo EndMe
    End If
    If MsgBox("Do you want the program to delete the unused sheets prior to Save?", vbYesNo + vbQuestion, "Delete Unused Sheets?") = vbYes Then
        Application.DisplayAlerts = False
        For Each sh In Sheets
          sh.Unprotect Password:="secret"
          If Application.CountIf(sh.Range("d4:d6"), "DELETEME") = 3 Then sh.Delete
            If Application.CountIf(sh.Range("d5:d7"), "DELETEME") = 3 Then sh.Delete
        Next
       sh.Protect Password:="secret"
      End If
    EndMe:
    End Sub

    Forgot to add: The sheets being deleted have DELETEME either in d4:d6 or d5:d7. Those cells are LName, FName, MName, and each sheet has that info in one of the two ranges. So some of the sheets use d4:d6 and others use d5:d7. That's why there those two lines in the code.

    VR/Lost
    Last edited by leaning; 07-01-2010 at 10:11 PM. Reason: forgot some stuff

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