Hi all, after posting in various forums trying to find a solution for my problem, searching google for hours, I am coming here to see if anyone can help me with this situation I am stuck in at work.
If someone can fix this problem for me, you will end the heartache I've had for the last 5 days, so any help is appreciated very VERY much.
For the sake of me finding my solution faster, I created an example spreadsheet displaying exactly what I need from my macros.
Below, you will see pictured 2 buttons, both with macros attached.
The "Protect Sheet" Button will call a macro that protects the entire sheet, locking down all cells, making this spreadsheet completely un-editable in this state.
The "Unprotect Sheet" button, SHOULD call a macro that Protects the entire sheet as well, but also sets an edit range (in this case A1:A5). (There are also lines in this code related to entering a password; these lines work fine and can be ignored; I am only concerned with what comes after the IF statement located after the password loop.)
HERES MY ISSUE:
"Protect sheet" button works fine. Press button, entire sheet locks and password is set.
Once I press the "Unprotect" button, my password box loads up and allows me to type it in, once I type it in it unlocks, but I get a run-time error on the line of code setting the edit ranges.
Basically, I just need help revising this code to something that will work; I think I'm pretty close to getting the solution already; but I am also pretty amateur at VBA and could be completely off on everything.
Above is the sheet with the 2 buttons, demonstrating what I need done from each of the buttons in the text box to the right of them.
Above is the code I used for the "Protect Sheet" button, figured this wouldn't be necessary to include since it works, but decided on it anyway in case it's interfering with the other button.
Above is the code I used for the "Unprotect Sheet" button. This is the code that is failing, and after testing more than 10 iterations of the same few lines of code, all doing the same thing but written in different ways, I am stumped.
Here are the lines that are failing.
Once again, I appreciate any possible solutions I can test for this, even if it doesn't fully help my problem, if you can get me one step closer to figuring out what's wrong, I don't know how I will repay.
If someone can fix this problem for me, you will end the heartache I've had for the last 5 days, so any help is appreciated very VERY much.
For the sake of me finding my solution faster, I created an example spreadsheet displaying exactly what I need from my macros.
Below, you will see pictured 2 buttons, both with macros attached.
The "Protect Sheet" Button will call a macro that protects the entire sheet, locking down all cells, making this spreadsheet completely un-editable in this state.
The "Unprotect Sheet" button, SHOULD call a macro that Protects the entire sheet as well, but also sets an edit range (in this case A1:A5). (There are also lines in this code related to entering a password; these lines work fine and can be ignored; I am only concerned with what comes after the IF statement located after the password loop.)
HERES MY ISSUE:
"Protect sheet" button works fine. Press button, entire sheet locks and password is set.
Once I press the "Unprotect" button, my password box loads up and allows me to type it in, once I type it in it unlocks, but I get a run-time error on the line of code setting the edit ranges.
Basically, I just need help revising this code to something that will work; I think I'm pretty close to getting the solution already; but I am also pretty amateur at VBA and could be completely off on everything.
Above is the sheet with the 2 buttons, demonstrating what I need done from each of the buttons in the text box to the right of them.
VBA Code:
Option Explicit
Sub ProtectSheet123()
ActiveSheet.Protect PassWord:="abc"
End Sub
Above is the code I used for the "Protect Sheet" button, figured this wouldn't be necessary to include since it works, but decided on it anyway in case it's interfering with the other button.
VBA Code:
Option Explicit
Sub UnprotectSheet123()
Dim PassWord As String, i As Integer
i = 0
Do
i = i + 1
If i > 3 Then
MsgBox "Only 3 Password Tries Allowed. Application will now save & close."
Application.DisplayAlerts = False
ThisWorkbook.Saved = True
Application.Visible = False
Application.Quit
Exit Sub
End If
PassWord = InputBox("Enter Password (Accessable by admin only)")
Loop Until PassWord = "abc"
If PassWord = "abc" Then
With Sheets("Testsheet")
.Select
.Protection.AllowEditRanges("EditableRange").Delete
.Protection.AllowEditRanges.Add Title:="EditableRange", Range:=Range("A1:A5"), PassWord:=""
End With
End If
End Sub
Above is the code I used for the "Unprotect Sheet" button. This is the code that is failing, and after testing more than 10 iterations of the same few lines of code, all doing the same thing but written in different ways, I am stumped.
Here are the lines that are failing.
VBA Code:
.Protection.AllowEditRanges("EditableRange").Delete
.Protection.AllowEditRanges.Add Title:="EditableRange", Range:=Range("A1:A5"), PassWord:=""
Once again, I appreciate any possible solutions I can test for this, even if it doesn't fully help my problem, if you can get me one step closer to figuring out what's wrong, I don't know how I will repay.