Consulting

Results 1 to 14 of 14

Thread: Solved: stopping users deleting sheets

  1. #1
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    291
    Location

    Solved: stopping users deleting sheets

    In Excel VBA is there a way to stop users from deleting or hiding or indeed adding sheets to a workbook i.e. is there a Worksheet_BeforeDelete event?

    If not how can I do this in VBA. Would I have to put code behind each sheet to stop it being deleted?

  2. #2
    Protect workbook
    but it's not infalible
    2+2=9 ... (My Arithmetic Is Mental)

  3. #3
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    291
    Location
    I'm not sure that would work as users need to use the workbook i.e enter data, do calcs, copy and paste so I cant protect the whole workbook

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If you put this in the ThisWorkbook code module.
    Dim newSh As Boolean
    
    Private Sub Workbook_NewSheet(ByVal Sh As Object)
     Application.DisplayAlerts = False
       newSh = True
       Sh.Delete
     Application.DisplayAlerts = True
    End Sub
    
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
      Me.Unprotect
      Call unHideAll
    End Sub
    
    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
      If newSh Then newSh = False: Exit Sub
      Me.Protect
    End Sub
    and this in a normal module
    Sub unHideAll()
    Dim xsheet As Worksheet
    For Each xsheet In ThisWorkbook.Sheets
        xsheet.Visible = xlSheetVisible
    Next xsheet
    End Sub
    It will prevent sheets from being added, deleted or hidden.
    It works because a sheet must be deactivated before it is deleted.

  5. #5
    Quote Originally Posted by philfer
    I'm not sure that would work as users need to use the workbook i.e enter data, do calcs, copy and paste so I cant protect the whole workbook
    Protecting the workbook is not the same as protecting sheets.
    When you protect the workbook you protect the workbook structure, how amy sheets, what they are called, if they are hidden. You can still enter data. Why not read up on a suggestion before you shoot it down ?
    2+2=9 ... (My Arithmetic Is Mental)

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    For goodness sake, he didn't shoot it down, he just made a comment on the response. A little explanation of why it might work might have been more constructive, but anyway philfer can ignore it because mikerickson seems to have given him a perfectly workable response.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Quote Originally Posted by xld
    For goodness sake, he didn't shoot it down, he just made a comment on the response. A little explanation of why it might work might have been more constructive, but anyway philfer can ignore it because mikerickson seems to have given him a perfectly workable response.
    Astute obeservation ...
    Seems to work if only they don't disable macros
    Some people want it both ways !
    2+2=9 ... (My Arithmetic Is Mental)

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    One more improvement in 2007, there is a facility to define trusted locations, so you can store all macro constrained workbooks there, and then turn your security setting to high.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    And where did he
    Say he was using 2007 ?
    Simply must have missed that !
    2+2=9 ... (My Arithmetic Is Mental)

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    He didn't, just an observation on my part.

    I was trying to introduce some relevance, some usefulness to the post. On reflection, I agree that my post was only relevant in response to yours, which as he hadn't mentioned that he disabled macros; and as he was posting on VBAExpress then he probably doesn't disable macros, there was probably initial relevance.

    I apologise, I won't bother in future.
    Last edited by Bob Phillips; 12-03-2007 at 04:33 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If users want to disable macros and deprive themselves of the benefits of my coding, it fine with me. Their computer, their decision.

  12. #12
    Quote Originally Posted by mikerickson
    If users want to disable macros and deprive themselves of the benefits of my coding, it fine with me. Their computer, their decision.
    Well it seems they want to deprive themselves of philfer's sheets and you would not believe some of the stuff users mess with 'cos they can' ... actually you probably would !!!
    2+2=9 ... (My Arithmetic Is Mental)

  13. #13
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If users want to mess with stuff 'cos they can', let 'em. It their data on their computer running the code that they decided to let manipulate their data.
    Protecting a user against their bad habits (eg.deleting a sheet without checking that the proper sheet is active.)is a feature of good coding (hence, warning screens), but there is no need to take it to extreems. If they want to deliberately input garbage, I don't see any problem when garbage comes out.

  14. #14
    I hear and applaud your point, but generally 'WE' get it in the neck because "Why did you let me do that ?"
    Look at the modern legal establishment where (and this is true) a user washed their poodle and dried it in the microwave, they sued the company and won (standard business school case point), now every plastic bag comes with instructions not to pull it over your head ! That's just reducing the number of Darwin awards, muddying up the gene pool and not giving the rest of us something to laugh at.
    I once had a multi-person, cross-departmental timesheet system, which was fully protected (as far as Excel allows ) pulled apart because the user wanted to populate a new year but didn't want to read the plentifull instructions about doing just that (cos the 1st if the month usually falls on a different day than the previous year (every time !!!). I also blame the tech guy who broke the password (the user had no chance of doing that !) for allowing a departmental reporting system to be tinkered with by an idiot studying to become a moron.
    Rather than enter the year in a designated cell and run the update script, they manually went in and changed every day for every user to a hardcoded value. I rate most users very highly, but some users seem bent on causing themselves pain.
    So I applaud your point of view but until PC's come with a loaded firearm pointed at the user and when a prompt comes up and says "Are you now going to do something Stupid ?" and they answer truthfully so the inquest verdict will be "Suicide by use of PC", then I won't hold my breath.
    2+2=9 ... (My Arithmetic Is Mental)

Posting Permissions

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