Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Macros on protected sheets

0

hi,

Attached has two sheets one "invoice" other one "invoice inverntory"  in the invoice sheet ill input data on unlocked cells whcih copies to inventory sheet as i run macro

if i protect the invoice sheet with the VBA to clear the data it doesnt clear cells as i wanted, but the other codes works

if i protect the inventory sheet NO MACROS runs ,it says sheet is protected

How to run macros on protected sheets? scondly why i cannot clear the data using macro on protected sheet (invoice)

kindly advise 

Thanks

Answer
Discuss

Answers

0
Selected Answer

Ramzein,

Please see (and try) the attached file.

If the Invoice sheet (Sheet13) is protected then you need to unlock the sheet before changingthe contents you need cleared.

The extra code in bold below will unlock the Invoice sheet (and re-lock it, allowing only unprotected cells to be entered). For easier use, I've added a string variable myPassword assuming you will use a password to make protection effective. (If not, you could replace myPassword with "" in the code).

The macro to copy data to the Inventory sheet works even if you protect the Invoice sheet.

Sub cleardata()
'
' cleardata Macro
'

'Dim myPassword As String

myPassword = "" 'If a password a*? is used to protect then change to ("a*?")

Sheet13.Activate 'In case macro is called from elsewhere
Sheet13.Unprotect Password:=myPassword 'Unlock sheet

    Range("G47,E20,H20,H34:H43,G34:G43,F34:F43,D34:D43,E49,E54").Select
    Range("E54").Activate
    Selection.ClearContents
    Range("D34").Select

Sheet13.Protect Password:=myPasword, Contents:=True ' Reapply the password here, allowing only unprotected cells to be selected

End Sub
Discuss

Discussion

Ramzein, forgot to say- I leave you to unprotect cells in the Invoice sheet that will probably need to be changed in practice: E21,E22 and E23 etc.

Also, in the Invoice sheet, I corrected the spelling of Diistributers to Distributors and Manufacturering to Manufacturing plus QUATATION to QUOTATION (Invoice inventory, H2)
John_Ru (rep: 6142) Nov 19, '20 at 5:01 am
hi John,

THe file you have uploaded has no such VBA may be u sent the old file of mine?
Ramzein (rep: 8) Nov 19, '20 at 7:11 am
Ramzein. Please check. The file I have attached should have the code under Module 1 modified as per my Answer. Also kindly try your buttons on the Invoice tab- they should work.
John_Ru (rep: 6142) Nov 19, '20 at 7:24 am
Hi John,

Thanks for correcting those mistakes in the invoice sheet, as you said it works but what im  expecting is to protect inventory sheet as well ( whole sheet) so do i need to apply this code to copy the data to inverntory sheet 11? 

Kindly view module 3 

ill add this code under "clear date" to unprotect the invoice in module3
how about inventory sheet? i think i have to copy this code again to unprotect the sheet 11 as well?
pls correct me if im wrong 

Thank you so much
Ramzein (rep: 8) Nov 19, '20 at 8:16 am
Ranzein. Yes you need to copy the unprotect and protect code into the macro for the Inventory sheet (but change references from sheet11 to sheet13.) Your question only referred to protecting the Invoice sheet so I didn't do that.

Once again, don't forget to mark the answer as Selected
John_Ru (rep: 6142) Nov 19, '20 at 8:36 am
hi, John,

Thanks for reply and i did already and it works on both sheets (sheet 11&13)
Dim mypassword as string" i didnt copy this as already declared for "clear data"

hope i did correct

"Once again, don't forget to mark the answer as Selected" OF COURSE I DONT FORGET

Thanks
Ramzein
Ramzein (rep: 8) Nov 19, '20 at 9:16 am
Thanks Ramzein. For completeness you should declare Dim mypassword as string in the other code too since the variable only has "life" within/ while a private sub is running. You will get away without the declaration since VBA  assumes varaibles are variant unless they as declared.
John_Ru (rep: 6142) Nov 19, '20 at 9:34 am
Add to Discussion
0

Excel macros are a great way to save time and eliminate errors. If you want to try to run your favorite Excel Macro on a Worksheet that has been protected, with or without a password. When such a Worksheet is encountered your macro may well no longer work and display a Run Time Error.

One way that many do use is like shown below

Sub MyMacro()

Sheet1.Unprotect Password:="Secret"

'YOUR CODE

Sheet1.Protect Password:="Secret"

End Sub

As you can see, the code un-protects Sheet1 with the password "Secret", run the code, and then the password protects it again. This will work but has a number of drawbacks with one being that the code could bug out and stop before it encounters the Sheet1. Protect Password:="Secret" line of code. This of course would mean your Worksheet fully unprotected. The other problem is that you would need a similar code for all Macros and all Worksheets.

Regards,

Rick Bale

Discuss
0

The UserInterFaceOnly is an optional argument of the Protect Method that we can set to True, the default is False. By setting this argument to True Excel will allow all Excel VBA macros to run on the Worksheet that protected with or without a password.

You need to be fully aware that, If you use the Protect method with the UserInterfaceOnly argument set to True on a Worksheet, then save the workbook, the entire worksheet (not just the interface) will be fully protected when you reopen the workbook. To set the user UserInterfaceOnly back to True after the workbook is opened, you must again apply the Protect method with UserInterfaceOnly set to True.

Best,
Jerry M.

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login