Link to home
Create AccountLog in
Avatar of McQMom
McQMomFlag for United States of America

asked on

Excel vba: hide sheets unless macros enabled and use workbook_beforesave event to prevent saving

Hi! I have a worksheet that we are concerned with the data's security. What I would like to do is hide all of the sheets except Sheet1 unless the macros are enabled. I would also like to use the Workbook_beforesave event codingto prevent the users from saving the data. I know I can do the latter by changing the file's permissions, butthe users will need to manipulate the pivit tables, so I can't do it that way. I've tried putting in the following codes but neither of them seem to be working at all - not even an error message. I'm clueless. Can someone please sort it out for me? Much thanks!  Experts-Exchange-Prevent-Save.xlsm
Private Sub Workbook_Open()
'If macros are disabled, only the Sheet1 is visible.
'If macros are enabled, the following code will run, hiding the Sheet1 and
'displaying the Pivot.

With ThisWorkbook
    With .Worksheets("Pivot")
        .Visible = xlSheetVisible
        .Select
    End With
    With .Worksheets("Sheet1")
        .Visible = xlSheetVeryHidden
    End With
End With

End Sub

Open in new window

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
     If SaveAsUI Then
         MsgBox "You cannot save a copy of this workbook!"
         Cancel = True
    End If
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Swapnil Nirmal
Swapnil Nirmal
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Chris Bottomley
Avatar of McQMom

ASKER

Thanks - that does it! As far as the saving goes, my message is popping up now, but it will let me do "Save As". Can I modify the macro to stop that as well?
Which code are you using as it will no doubt need to match the implementation BUT in the workbook code module use:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    if saveasui then cancel = true
end sub

Although needs to allow for any existing code in the module

Chris
Avatar of McQMom

ASKER

I'm using the code in Module 3:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
     If SaveAsUI Then
         MsgBox "You cannot save a copy of this workbook!"
         Cancel = True
    End If
End Sub

That's it, what's the problem with that then?
Avatar of McQMom

ASKER

If I click on "Save" it shows the message box and won't let me save (perfect!). When I click on 'Save As" it lets me do it (not good!)
are you sure you the ode as presented in the specific workbook module?

Chris
Avatar of McQMom

ASKER

Well, I took the actual file that N swapnil corrected above.
LOok in the workbook code module ... the sense is the other way around there.

Chris
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of McQMom

ASKER

Since the macro is a private sub, I don't know which one is triggering. I have it in the Workbook module, module 3 & Sheet4. Should it only be in the workbook module?
Avatar of McQMom

ASKER

I put in your code but now it won't let me save the code changes because of the code....
That sub is ONLY relevant to the thisworkbook in regard to working off the save / saves event ... and if you had tried to implement the method via the link as I posted you will have a better understanding.  But then again I prefer teaching to presenting a fait accompli when possible.

Key is only the thisworkbook version is triggered on save / save as event

Chris
:)

What do you want ... all save and save as prevented or just save as?

Chris
Avatar of McQMom

ASKER

I did look at the link, but vba is pretty much an unknown frontier for me. :(
Just as an aside, the advantage with a set of instructions is you can reproduce it time and time again.

Chris
Avatar of McQMom

ASKER

THank you guys! Chris - I ended up playing with your last macro. I made it so the user can save but gets a message telling them not to, but stopping them from Save As. I figure this will be the best bet to stop the workbook from travelling onto another hard drive. :)
Okay you have decided on your way ... but given you want to stop both saves, what you do is enable the ban in the script THEN in the VBE select the pause, (break) button, now save then press the stop and macros are again enabled ... but now with the workbook save inhibited .... unless the user is VBA aware of course.

Chris