Allowing VBA code to make changes, even when sheet is protected

pijan11

New Member
Joined
Jan 29, 2022
Messages
29
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I'm using this code to allow to make changes to a protected sheet with a VBA code:

VBA Code:
Sub AllowVBAChangesOnProtectedSheet()

'Enable changes to worksheet by VBA code, even if protected
Sheets("Sheet1").Protect Password:="myPassword", _
    UserInterfaceOnly:=True

End Sub

Every time I close my excel file and return to it, I always have to run this code manually before inputting any data in my spreadsheet. If I don't I get a Run-time error '1004'.

Any help would be appeciated.
 

Attachments

  • Screen Shot 2022-01-29 at 10.20.47 PM.png
    Screen Shot 2022-01-29 at 10.20.47 PM.png
    107.5 KB · Views: 18
Last edited by a moderator:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Welcome to the MrExcel board!

Remove that code and put this code in the ThisWorkbook module instead.

VBA Code:
Private Sub Workbook_Open()
  'Enable changes to worksheet by VBA code, even if protected
  Sheets("Sheet1").Protect Password:="myPassword", _
    UserInterfaceOnly:=True
End Sub

1643515797736.png


BTW, when posting vba code, please use the available code tags. My signature block below has more details. I fixed it for you this time in your post above. :)
 
Upvote 0
Solution
Welcome to the MrExcel board!

Remove that code and put this code in the ThisWorkbook module instead.

VBA Code:
Private Sub Workbook_Open()
  'Enable changes to worksheet by VBA code, even if protected
  Sheets("Sheet1").Protect Password:="myPassword", _
    UserInterfaceOnly:=True
End Sub

View attachment 56455

BTW, when posting vba code, please use the available code tags. My signature block below has more details. I fixed it for you this time in your post above. :)
Thanks Peter. It's working. Now how would I add another code so that it does the same thing to another protected sheet ?
 
Upvote 0
If the other sheet is in the same workbook but has a different password, you could use

VBA Code:
Private Sub Workbook_Open()
  'Enable changes to worksheet by VBA code, even if protected
  Sheets("Sheet1").Protect Password:="myPassword", UserInterfaceOnly:=True
  Sheets("Other Sheet").Protect Password:="otherPassword", UserInterfaceOnly:=True
End Sub

If there were multiple sheets and they all used the same password a loop could be used to apply the protection.
 
Upvote 0
If the other sheet is in the same workbook but has a different password, you could use

VBA Code:
Private Sub Workbook_Open()
  'Enable changes to worksheet by VBA code, even if protected
  Sheets("Sheet1").Protect Password:="myPassword", UserInterfaceOnly:=True
  Sheets("Other Sheet").Protect Password:="otherPassword", UserInterfaceOnly:=True
End Sub

If there were multiple sheets and they all used the same password a loop could be used to apply the protection.
And how do you apply a loop? Sorry for all the questions. I’m a beginner.
 
Upvote 0
First, make sure all the passwords for the relevant sheets are the same then

VBA Code:
Private Sub Workbook_Open()
  'Enable changes to worksheet by VBA code, even if protected
  Dim itm As Variant
  
  For Each itm In Array("Sheet1", "Other Sheet", "Data")
    Sheets(itm).Protect Password:="myPassword", UserInterfaceOnly:=True
  Next itm
End Sub
 
Upvote 0
Thank you very much Peter. Since you're the only one replying to me (and I really appreciate it) would you mind looking at my other post entitles Excel Running Slow?


If you have time
 
Upvote 0
would you mind looking at my other post entitles Excel Running Slow?
. I try to look at as many threads as I can, and if I come across yours and think I can help, I will.
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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