Forum Discussion
Protecting VBA code even if copied into new workbook
Hi Kenny15,
The protection tools in Excel are really weak and can be easily hacked.
I have asked the Excel team about this issue before in this https://excel.uservoice.com/forums/304921-excel-for-windows-desktop-application/suggestions/32041498-make-excel-protection-tools-more-reliable-and-fool.
With regards to the flaw you've mentioned, the worksheet that has a code should be blocked from copying and moving into another workbook or at least moving or copying without carrying the code.
It's impossible to keep the protection after you copy the worksheet because the protection is on the whole VBA project level, not on the worksheet level.
However, you have an option in the Review tab to protect the workbook to prevent its worksheets from copying or moving in addition to other restrictions.
There is no way at this time to be able to copy or move a protected worksheet into another workbook and keep the worksheet code protected.
If don't want to use the Protect Workbook feature in the Review tab, I have a suggestion for you, it's to refactor the code in the workbook so that you keep the main methods and functions outside the worksheets code module.
You can store them in separate modules or class modules and call them from the worksheets code modules.
Example
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call test
End Sub
The test is a sub stored in a separate module outside the worksheet1 code module.
Sub test()
MsgBox "Hello World!"
End Sub
Regards,
Haytham
- Kenny15Apr 08, 2019Copper Contributor
Thanks, Haytham. How do you use the review tab to protect the sheet from being copied
- Kenny15Apr 08, 2019Copper Contributor
Thank, Haytham. How do you use the review tab to protect the sheet from being copied
- Haytham AmairahApr 08, 2019Silver Contributor
This is can be done using the https://support.office.com/en-us/article/protect-a-workbook-7e365a4d-3e89-4616-84ca-1931257c1517 under the Protect group in the Review tab.
- Kenny15Apr 08, 2019Copper Contributor