VBA protect workbook

vbaNewby

Board Regular
Joined
Jan 26, 2011
Messages
138
Hi,

I am starting with a protected workbook. I then run a macro. I know you can at the start of the macro, unprotect it and then at the end protect it but this leaves room for an unprotected workbook if the macro crashes.

Code:
ActiveWorkbook.Unprotect Password:="xxx"
ActiveWorkbook.protect Password:="xxx"
So to get rid of that scenario I want to only allow the macro to work with the workbook.

I've tried:
Code:
ActiveWorkbook.Protect userinterfaceonly:=True
I get an error. Plus this doesn't make sense, since the workbook is protected from the get-go (before the macro makes this call)

Any solutions without un-protecting and then re-protecting?

Thanks in advance. Kind regards.
 

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.
Hi. Perhaps like this

Code:
On Error GoTo ErrHandl
ActiveWorkbook.Unprotect Password:="xxx"
'
'your code here
'
ErrHandl:
ActiveWorkbook.Protect Password:="xxx"
 
Upvote 0
A possibility would be to put an error handler in there:

Code:
Public Sub YourMacro()
On Error Goto CleanUp
'your code
CleanUp:
   ActiveWorkbook.Protect Password:="xxx"
   End Sub
 
Upvote 0
Hi. Perhaps like this

Code:
On Error GoTo ErrHandl
ActiveWorkbook.Unprotect Password:="xxx"
'
'your code here
'
ErrHandl:
ActiveWorkbook.Protect Password:="xxx"
Hi vog! This seems like a possible solution. Only thing is if I have multiple subs and I wrap my code around this in my main sub, don't I have to do this for every small sub that my main sub calls? (just in case my other subs have an error)

! hope that makes sense.
 
Upvote 0
Yes, you would need in each sub

Rich (BB code):
On Error GoTo ErrHandl

'your code here
'
Exit Sub
ErrHandl:
ActiveWorkbook.Protect Password:="xxx"
 
Upvote 0
A possibility would be to put an error handler in there:

Code:
Public Sub YourMacro()
On Error Goto CleanUp
'your code
CleanUp:
   ActiveWorkbook.Protect Password:="xxx"
   End Sub
Thanks MrKowz, please seem my previous post for my concerns with this.
 
Upvote 0
Yes, you would need in each sub

Rich (BB code):
On Error GoTo ErrHandl

'your code here
'
Exit Sub
ErrHandl:
ActiveWorkbook.Protect Password:="xxx"
Thanks Vog, seems a little cumbersome but I guess this is the best possible solution.

Kind regards.
 
Upvote 0
Yes, you would need in each sub

Rich (BB code):
On Error GoTo ErrHandl
 
'your code here
'
Exit Sub
ErrHandl:
ActiveWorkbook.Protect Password:="xxx"

VoG, would this also prevent the user from bypassing the error handler by breaking the code manually with Esc?
 
Upvote 0
VoG, would this also prevent the user from bypassing the error handler by breaking the code manually with Esc?


Hi there. I don't think so. You would need

Code:
Application.EnableCancelKey = xlErrorHandler
On Error GoTo ErrHandl
 
'your code here
'
Exit Sub
ErrHandl:
ActiveWorkbook.Protect Password:="xxx"
Application.EnableCancelKey = xlInterrupt
 
Upvote 0
Hi there. I don't think so. You would need

Code:
Application.EnableCancelKey = xlErrorHandler
On Error GoTo ErrHandl
 
'your code here
'
Exit Sub
ErrHandl:
ActiveWorkbook.Protect Password:="xxx"
Application.EnableCancelKey = xlInterrupt

MrKowz. Great point. Vog thanks for the additional tip.
 
Upvote 0

Forum statistics

Threads
1,224,544
Messages
6,179,430
Members
452,915
Latest member
hannnahheileen

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