Home

Protecting VBA code even if copied into new workbook

%3CLINGO-SUB%20id%3D%22lingo-sub-407868%22%20slang%3D%22en-US%22%3EProtecting%20VBA%20code%20even%20if%20copied%20into%20new%20workbook%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-407868%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20situation%20that%20may%20actually%20be%20a%20flaw%20in%20Excel.%26nbsp%3B%20I%20have%20a%20workbook%20where%20I%20have%20some%20VBA%20code.%26nbsp%3B%20I%20have%20password%20protected%20the%20code.%20In%20the%20same%20workbook%20I%20have%20code%20on%20the%20individual%20sheet%20by%20right%20clicking%20on%20the%20tab%20and%20select%20view%20code%20where%20I%20enter%20the%20code.%26nbsp%3B%20When%20I%20password%20protect%20the%20workbook%20code%20by%20going%20into%20tools%20menu%20and%20selecting%20VBAproject%20properties%20clicking%20on%20the%20protection%20tab%20and%20entering%20a%20password.%26nbsp%3B%20This%20will%20also%20protect%20the%20code%20I%20entered%20on%20the%20view%20code%20tab%20of%20the%20worksheet%20as%20well.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20codes%20stays%20protected%20in%20the%20workbook.%26nbsp%3B%20However%20if%20someones%20copies%20the%20sheet%20we%20have%20had%20the%20code%20into%20a%20new%20workbook%20the%20password%20protection%20is%20gone%20and%20they%20can%20now%20view%20the%20code.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOn%20this%20same%20sheet%20I%20have%20password%20protected%20certain%20cells%20and%20when%20they%20copy%20the%20same%20sheet%20the%20password%20protection%20stays%20for%20the%20locked%20cells%20but%20not%20the%20code%20when%20copied%20into%20a%20new%20workbook.%26nbsp%3B%20Lastly%20the%20code%20that%20I%20had%20protected%20no%20longer%20works%20when%20also%20copied%20into%20a%20new%20workbook.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20the%20following%20code%20on%20the%20this%20workbook%20and%20when%20they%20copy%20the%20sheet%20into%20a%20new%20workbook%20the%20code%20no%20longer%20works%20as%20well%20as%20the%20passwords.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20Workbook_Open()%3CBR%20%2F%3EDim%20sh%20As%20Worksheet%3CBR%20%2F%3EFor%20Each%20sh%20In%20ThisWorkbook.Worksheets%3CBR%20%2F%3E'%20Add%20condition%20for%20specifying%20sheet%20names%20if%20needed%3CBR%20%2F%3EIf%20Date%20%26gt%3B%20%231%2F1%2F2020%23%20Then%3CBR%20%2F%3Esh.EnableCalculation%20%3D%20False%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3ENext%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20can%20I%20keep%20the%20password%20protection%20even%20if%20someone%20copies%20the%20sheet%20into%20a%20new%20workbook%20and%20also%20make%20sure%20the%20code%20still%20applies%20when%20copied%20into%20a%20new%20workbook.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-407868%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-408629%22%20slang%3D%22en-US%22%3ERe%3A%20Protecting%20VBA%20code%20even%20if%20copied%20into%20new%20workbook%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-408629%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F316029%22%20target%3D%22_blank%22%3E%40Kenny15%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20protection%20tools%20in%20Excel%20are%20really%20weak%20%3CSPAN%20class%3D%22tlid-translation%20translation%22%3E%3CSPAN%20class%3D%22%22%3Eand%20can%20be%20easily%20hacked.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EI%20have%20asked%20the%26nbsp%3BExcel%20team%20about%20this%20issue%20before%20in%20this%20%3CA%20href%3D%22https%3A%2F%2Fexcel.uservoice.com%2Fforums%2F304921-excel-for-windows-desktop-application%2Fsuggestions%2F32041498-make-excel-protection-tools-more-reliable-and-fool%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Elink%3C%2FA%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20regards%20to%20the%20flaw%20%3CSPAN%20class%3D%22tlid-translation%20translation%22%3E%3CSPAN%20class%3D%22%22%3Eyou've%20mentioned%3C%2FSPAN%3E%3C%2FSPAN%3E%2C%20the%20worksheet%20that%20has%20a%20code%20should%20be%20blocked%20from%20copying%20and%20moving%20into%20another%20workbook%20or%20at%20least%20moving%20or%20copying%20without%20carrying%20the%20code.%3C%2FP%3E%3CP%3EIt's%20impossible%20to%20keep%20the%20protection%20after%20you%20copy%20the%20worksheet%20because%20the%20protection%20is%20on%20the%20whole%20VBA%20project%20level%2C%20not%20on%20the%20worksheet%20level.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%20class%3D%22text-wrap%20tlid-copy-target%22%3E%3CDIV%20class%3D%22result-shield-container%20tlid-copy-target%22%3E%3CSPAN%20class%3D%22tlid-translation%20translation%22%3E%3CSPAN%3EThe%20solution%20is%20to%20have%20a%20VBA%20protection%20feature%20that%20works%20on%20the%20worksheet%20level.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%20class%3D%22result-shield-container%20tlid-copy-target%22%3E%3CSPAN%20class%3D%22tlid-translation%20translation%22%3E%3CSPAN%3E%3CSPAN%20class%3D%22%22%3EAnd%20this%20is%20what%20we%20are%20missing%20now%3C%2FSPAN%3E!%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%20class%3D%22result-shield-container%20tlid-copy-target%22%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3EHowever%2C%20you%20have%20an%20option%20in%20the%20Review%20tab%20to%20protect%20the%20workbook%20to%20prevent%20its%20worksheets%20from%20copying%20or%20moving%20in%20addition%20to%20other%20restrictions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20is%20no%20way%20at%20this%20time%20to%20be%20able%20to%20copy%20or%20move%20a%20protected%20worksheet%20into%20another%20workbook%20and%20keep%20the%20worksheet%20code%20protected.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20don't%20want%20to%20use%20the%20Protect%20Workbook%20feature%20in%20the%20Review%20tab%2C%20I%20have%20a%20suggestion%20for%20you%2C%20it's%20to%20refactor%20the%20code%20in%20the%20workbook%20so%20that%20you%20keep%20the%20main%20methods%20and%20functions%20outside%20the%20worksheets%20code%20module.%3C%2FP%3E%3CP%3EYou%20can%20store%20them%20in%20separate%20modules%20or%20class%20modules%20and%20call%20them%20from%20the%20worksheets%20code%20modules.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EExample%3C%2FSTRONG%3E%3C%2FP%3E%3CPRE%3EPrivate%20Sub%20Worksheet_SelectionChange(ByVal%20Target%20As%20Range)%3CBR%20%2F%3E%20%20Call%20test%3CBR%20%2F%3EEnd%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20%3CSTRONG%3Etest%20%3C%2FSTRONG%3Eis%20a%20sub%20stored%20in%20a%20separate%20module%20outside%20the%20worksheet1%20code%20module.%3C%2FP%3E%3CPRE%3ESub%20test()%3CBR%20%2F%3E%20MsgBox%20%22Hello%20World!%22%3CBR%20%2F%3EEnd%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%2C%3C%2FP%3E%3CP%3EHaytham%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-409992%22%20slang%3D%22en-US%22%3ERe%3A%20Protecting%20VBA%20code%20even%20if%20copied%20into%20new%20workbook%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-409992%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F35679%22%20target%3D%22_blank%22%3E%40Haytham%20Amairah%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%20Haytham.%26nbsp%3B%20How%20do%20you%20use%20the%20review%20tab%20to%20protect%20the%20sheet%20from%20being%20copied%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-410109%22%20slang%3D%22en-US%22%3ERe%3A%20Protecting%20VBA%20code%20even%20if%20copied%20into%20new%20workbook%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-410109%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20is%20can%20be%20done%20using%20the%20%3CA%20href%3D%22https%3A%2F%2Fsupport.office.com%2Fen-us%2Farticle%2Fprotect-a-workbook-7e365a4d-3e89-4616-84ca-1931257c1517%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EProtect%20Workbook%3C%2FA%3E%20under%20the%20Protect%20group%20in%20the%20Review%20tab.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-410937%22%20slang%3D%22en-US%22%3ERe%3A%20Protecting%20VBA%20code%20even%20if%20copied%20into%20new%20workbook%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-410937%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F35679%22%20target%3D%22_blank%22%3E%40Haytham%20Amairah%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eok%20thanks%3C%2FP%3E%3C%2FLINGO-BODY%3E
Kenny15
New Contributor

I have a situation that may actually be a flaw in Excel.  I have a workbook where I have some VBA code.  I have password protected the code. In the same workbook I have code on the individual sheet by right clicking on the tab and select view code where I enter the code.  When I password protect the workbook code by going into tools menu and selecting VBAproject properties clicking on the protection tab and entering a password.  This will also protect the code I entered on the view code tab of the worksheet as well.

 

The codes stays protected in the workbook.  However if someones copies the sheet we have had the code into a new workbook the password protection is gone and they can now view the code. 

 

On this same sheet I have password protected certain cells and when they copy the same sheet the password protection stays for the locked cells but not the code when copied into a new workbook.  Lastly the code that I had protected no longer works when also copied into a new workbook.

 

I have the following code on the this workbook and when they copy the sheet into a new workbook the code no longer works as well as the passwords.  

 

Private Sub Workbook_Open()
Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
' Add condition for specifying sheet names if needed
If Date > #1/1/2020# Then
sh.EnableCalculation = False
End If
Next
End Sub

 

How can I keep the password protection even if someone copies the sheet into a new workbook and also make sure the code still applies when copied into a new workbook.

 

 

5 Replies

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 link.

 

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.

 

The solution is to have a VBA protection feature that works on the worksheet level.
And this is what we are missing now!
 

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

@Haytham Amairah 

 

Thank, Haytham.  How do you use the review tab to protect the sheet from being copied

@Haytham Amairah 

 

Thanks, Haytham.  How do you use the review tab to protect the sheet from being copied

This is can be done using the Protect Workbook under the Protect group in the Review tab.

Related Conversations
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
2 Replies
*Updated 9/3* Syncing in Microsoft Edge Preview Channels
Elliot Kirk in Articles on
202 Replies
Early preview of Microsoft Edge group policies
Sean Lyndersay in Discussions on
65 Replies