I wrote a code for the worksheet, and everytime I change a number, if the varaince is greater than by 10%, 20%, or +20% it changes color. So i’ve used macro code on both “General” and “Private Sub Selection_Worksheet” so that everytime the cell’s number changes, it would change the color.
I’ve locked and hidden some of the cells that show formulas,and wrote a code
Sheets("Financials").Protect Password:="Ottawa", UserInterFaceOnly:=True
on both General and “Private Sub Selection_Worksheet” and it gives me a runtime error everytime i click on any cell.
How do I solve this issue? I only want them to use certain cells and still run the macro while being protected and hidden.
Thanks,
Daniel
The protection with
UserInterface:=Trueis unfortunately somewhat misleading: If you reopen the file after saving, that status gets lost and the worksheet is again fully protected.Therefore, it’s better to change your code to handle the protection, too:
Private Const cStrPwd as String = "Ottawa" Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'Your conditions to check if code should run go here, e.g.: If Application.Intersect(Target, Me.Range("A1:B10")) Is Nothing Or _ Me.Range("B2") < 0.2 Then Exit Sub Me.Unprotect Password:=cStrPwd 'Your code here MsgBox "You've successfully done something! Nice job, mate!" Me.Protect Password:=cStrPwd End Sub