Prevent Workbook From Opening (VBA)

markchap

New Member
Joined
Jan 21, 2016
Messages
1
We have a workbook that we don't want given to unauthorized users. Each time anyone attempts to open the workbook, our code will check an online SQL table to make sure certain environmental variables (and company name) are equal to the approved values. If that test fails, we want the workbook first to display a message to the user and then NOT to open. Naturally, we don't want the user to be able to hit a key combination (such as Ctrl-Break) to interrupt the macro so that the workbook can be used.

Someone else is writing the code to check the online SQL table for approved values. I've tried putting my part of the code in the On Open event, but when my dialog box (using msgbox) displayed to inform the user that the security check failed, I was able to Ctrl-Break out of that code and right into Excel's debug/end dialog box and then into the very spreadsheet whose access was to be prohibited.

If it matters, I tested this before locking the VBA code. I will do so after finishing the code.

If more details are required, please ask. My preliminary code worked as planned (workbook never opened) as long as I didn't try to Ctrl-Break out of it. We want to make sure that this workbook cannot be used even if an unauthorized individual gets a hold of it.

If there are any other "gotchas", I'd appreciate being told about them.

Thanks as always!!!!
Mark
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Your scheme is fundamentally flawed in that is requires the user to allow your workbook's macro to run; if they don't it will open without any attempt at verification. And, as you've discovered, users who let the macro run can also Ctrl-Break out of the code before it's finished.
 
Upvote 0
Mark,

You can handle the scenario you describe by using this statement...

Code:
 Application.EnableCancelKey = xlErrorHandler

...which will send execution to the active error handler instead of to the debugger.

That being said, be aware that Excel worksheet protection and VBA Project protection provides relatively limited security.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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