Prevent VBA editor from being opened

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,898
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
Hi All
A colleague that I have written a workbook for has an issue with one of the users trying to open the VBA editor and see the code.
So it got me to thinking....is there a way to close the workbook if the VBE is opened.
I was thinking along the lines of when the press ALT + F11 a msgbox tells them they are unauthorised to view this page, and then closes the workbook without saving.

I haven't seen this done anywhere before, so it is a curiosity question at the moment.
The project in question does have a VBA password at the moment, so it's not a major problem yet !!

Any suggestions / inputs appreciated.
 
To Jaafar Tribak. I appreciate many of your posts and code. Regarding your code here, can you also make it Prevent the View Macros (Alt + F8) window from being opened? I need this as the user can load another (his) workbook in the same Excel instance and from this window to run his macros. In this way i hope to completely stop the user to run his macros from his workbook as long as my workbook and his are in the same instance of excel.
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
To Jaafar Tribak. I appreciate many of your posts and code. Regarding your code here, can you also make it Prevent the View Macros (Alt + F8) window from being opened? I need this as the user can load another (his) workbook in the same Excel instance and from this window to run his macros. In this way i hope to completely stop the user to run his macros from his workbook as long as my workbook and his are in the same instance of excel.
The only workaraound I can think of is to disable the Macros button that is located in the Developpers tab by modifying the Ribbon XML. You can make this dynamic so the Macros Button is only disbaled when the user's workbook is loaded and enabled back when closed. You can preferably do this in an add in or personal workbook.
I am ot sure if ALT+F8 will be disabled along with the Macros button after miodifying the ribbon xml ... If it is not disabled then you can simply disbale it with Application.OnKey "%{F8}", "" You can apply this dynamically as well.
 
Upvote 0
The only workaraound I can think of is to disable the Macros button that is located in the Developpers tab by modifying the Ribbon XML. You can make this dynamic so the Macros Button is only disbaled when the user's workbook is loaded and enabled back when closed. You can preferably do this in an add in or personal workbook.
I am ot sure if ALT+F8 will be disabled along with the Macros button after miodifying the ribbon xml ... If it is not disabled then you can simply disbale it with Application.OnKey "%{F8}", "" You can apply this dynamically as well.
To Jaafar Tribak . Yet your code from above, block the Vbe window to to appear regardless if i press the button or use Alt+F11 . I tried to change your code for Alt+ F8 and also the View Macro button from the developer tab but without success. I end up only to block the same Vbe window. Here is my modified useless code :

Public Sub Hide_VBA_run(): Application.SendKeys "%{F8}": lCBTHook = SetWindowsHookEx(WH_CBT, AddressOf CBTProc, GetAppInstance, GetCurrentThreadId): End Sub

I guess i need different values or different approach for SetWindowsHookEx, but that is beyond my knowledge . I am glad that at least i can understand a portion of your code .
Please can you modify that portion of your code in order to prevent the View Macros (Alt + F8) window from being opened like you had done in your code from above . Thank you again for your great work .
 
Upvote 0
@biovio
your code from above, block the Vbe window to to appear regardless if i press the button or use Alt+F11 . I tried to change your code for Alt+ F8 and also the View Macro button from the developer tab but without success
The hooking approach I took in post#3 won't work for not displaying the Macros dialog ... To be more specific, it could work but the code would need to be placed in a dll. This would need a lot of work.
As I said, the best (easier and more stable) approach is to modify the ribbon xml ... Look in the link below for how to modify the ribbon:
Once the xml ribbon is modified, the Macros button in the developper tab will remain disabled hence will prevent the user from displaying the Macros dialog.

Also, to just block ALT+F8, simply run the following line :
VBA Code:
Application.OnKey "%{F8}", ""

Hopefully, you can do the above without much effort. If you have any issues let us know.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,226,114
Messages
6,189,052
Members
453,522
Latest member
Seeker2025

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