Protect workbook during crash

jpen

Active Member
Joined
Jun 19, 2002
Messages
401
Hi everyone,

I have a workbook with VBA code. One of the worksheet is Proteced and the VBA code is Protected.

The VBA code is unprotecting the worksheet and afterwards protect it again.

The programm is loading data from an external source (*.txt files). Sometimes the Excel file crashes due to the fact that an user has no right to access a direcory or file.
In this case the when re-opening Excel the worksheet is unprotected. After running VBA code the worksheet is portected again.


How can I change the VBA code so the worksheet is protect after a crash?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
The VBA code is unprotecting the worksheet and afterwards protect it again.
Maybe this approach.

Also add the code that you have that protects it again to the BeforeSave event in VBA. Then, every time anyone tries to save the file, it will re-protect that code.
So, assuming that they haven't disabled VBA, it will be impossible to save the file with it unprotected.
 
Last edited:
Upvote 0
Alternatively, put something in a Workbook_Open event that protects the sheet.
That way as long as macros are enabled, the sheet will be protected.
 
Upvote 0
Alternatively, put something in a Workbook_Open event that protects the sheet.
That way as long as macros are enabled, the sheet will be protected.
I considered that too, however I like the BeforeSave is the better option. Here is why:

Both of these methods are subject to "Macros/VBA" being enabled. If it is first unprotected by VBA code running, then the only which in which it could/should become unprotected is if they have already enabled VBA code. So since we know that they have already enabled VBA code, then we know that the BeforeSave code should fire/work, and they will not be able to Save the file in an unprotected state.

However, if the file is unprotected, and they Open it up, if they do not elect to Enable VBA/Macros, they will be able to open the file in an unprotected state.
So, if you have a user whose goal was to try to open it up in an unprotected state, it seems it would be much easier to do if the code was in the Open event instead of the BeforeSave event. So the BeforeSave event would seem to be a more effective approach, in my opinion.

Or, if you REALLY wanted to have it as protected as possible, you could add the code to BOTH events (Open and BeforeSave)...
 
Last edited:
Upvote 0
Totally agree with you Joe, the only reason I suggested an Open event is because of this
How can I change the VBA code so the worksheet is protect after a crash?
If Xl crashes I doubt that the BeforeSave will fire.
 
Upvote 0
If Xl crashes I doubt that the BeforeSave will fire.
Very true, but if it never Saves, then it should never save the file in the state where it is left unprotected!
So, when they open up the file the next time, it will be in the previous saved state, that was before when they attempted to run the code that crashed it.
 
Last edited:
Upvote 0
Excellent point Joe
Never thought of that :(
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,581
Members
449,174
Latest member
chandan4057

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top