Creating a Macro that will Lock everything besides data-entry cells

evxret

New Member
Joined
Apr 8, 2022
Messages
38
Office Version
  1. 365
Platform
  1. Windows
Hi all, I had help creating a VB macro that is attached to buttons that will lock or unlock a workbook based on a macro-attached button press. I got the buttons to function the way they need to, the problem lies now that when I press "unprotect", the entire sheet is left vulnerable to formula & other structural changes when users need to add their data entry before pressing the "Protect" button. I want them to be able to save and lock their numbers, and they cannot be changed unless they ask me first. I hope im making sense. Basically I need help editing the lines in the attached code so instead of fully unprotecting the sheet, it unprotects only cells that I want to be able to be edited. Im not sure if this is possible, but I appreciate any and all help with this.

Here is the attached code:
VBA Code:
Option Explicit


Sub ProtectSheet()
    Sheet1.Protect PassWord:="abc"
End Sub

Sub UnprotectSheet()
Dim PassWord As String, i As Integer
  
  i = 0

Do
    i = i + 1
    If i > 5 Then
      MsgBox "Password may only be entered 5 times. Application will now 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
     Sheet1.Unprotect PassWord:="abc"
    End If
    
End Sub

I basically need the part that says "Sheet1.Unprotect PassWord" to be changed to something that will instead keep protection on everything besides data entry cells.
 
You are correct:
With Sheets("Sheet1")

I had selected sheet1 before that example but somehow the exact version didn't make it into my post. Sorry about that.
I'm getting an error 438 "object doesn't support this property or method" but have something I need to do right now.
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You are correct:
With Sheets("Sheet1")

I had selected sheet1 before that example but somehow the exact version didn't make it into my post. Sorry about that.
I'm getting an error 438 "object doesn't support this property or method" but have something I need to do right now.
Im so sorry; I feel so stupid.. Im still doing something wrong but I changed to your fix

1649704658635.png

Getting a syntax error on the line starting with "Select.Sheet1.Protection" highlighted in red.
 
Upvote 0
No you're not. Access vba is my thing - trying to learn Excel by helping out here more, so it's me and it's probably very simple. Trying to cook in the meantime too!
Question is, did that line ever error on you before or did your code never get that far?
Working on it, but maybe somebody will beat me to the answer. That was the line that caused the error I referred to in my last post.

EDIT
remove Sheet1 in that line. The With Block has already created a reference to that sheet. By repeating it, you're saying With Sheet1.Sheet1 and there is no such thing.
Took 5 seconds after that post to spot it. There is just enough similarity between Access and Excel vba to get me into trouble!

VBA Code:
With Sheets("Sheet1")
  .Select
  .Protection.AllowEditRanges("EditableRange").Delete
  .Protection.AllowEditRanges.Add Title:="EditableRange", Range:=Range("A1:A5"), PassWord:=""
End With
Probably don't have to select the sheet first, either.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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