Help with VBA Code to to unlock and lock sheet when using a command a command

JMC57

Board Regular
Joined
Apr 24, 2012
Messages
118
I have these two commands that appeared to work previously . Any help will be appreciated Thank you

Private Sub CommandButton1_Click()
Range("A2:j6000").ClearContents
End Sub

Private Sub CommandButton1_Click()
Range("e2:e6000").ClearContents
End Sub

Other commands on the sheet are as follows

Private Sub CommandButton3_Click()

Dim sh As Worksheet, lr As Long, rng As Range
Set sh = ActiveSheet
lr = sh.Cells(Rows.Count, 2).End(xlUp).Row
sh.Unprotect "jmc"
sh.Rows.Hidden = False
sh.Range("D1:D" & lr).AutoFilter 1, "C", , , False
Set rng = sh.Range("D2:D" & lr).SpecialCells(xlCellTypeVisible).EntireRow
sh.Range("D1:D" & lr).AutoFilter
rng.Hidden = True
ActiveSheet.Protect "jmc"
End Sub



Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Intersect(Target, Range("a2:k6000")) Is Nothing Then Exit Sub
Application.EnableEvents = False
Me.sort.SortFields.Clear
Me.sort.SortFields.Add Key:=Range("b2:b6000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With Me.sort
.SetRange Range("A2:k6000")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Application.EnableEvents = True

End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
There is a couple different ways to handle this. You can either unlock the sheet every time you need a macro to change something or you can protect the sheet with the UserInterfaceOnly parameter.

If you unlock the sheet for a macro and the macro fails, you could leave the sheet unprotected. The UserInterfaceOnly parameter allows a macro to edit a sheet without unprotecting it. The only drawback is that each time you load the workbook and each time you reset the VBA project, you must protect the sheet with that parameter again. That's why I run the macros below each time the sheet is activated and each time the workbook is opened. Hope this helps.

VBA Code:
Private Sub CommandButton1_Click()
    Activsheet.Unprotect "password"
   
    Range("A2:j6000").ClearContents
   
    Activsheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, _
    AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowSorting:=True, _
    AllowFiltering:=True, AllowUsingPivotTables:=True, Password:="password"
End Sub


VBA Code:
Private Sub Worksheet_Activate()
  Dim Sht As Worksheet
 
  Set Sht = ActiveSheet
 
  Sht.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, _
    AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowSorting:=True, _
    AllowFiltering:=True, AllowUsingPivotTables:=True, UserInterfaceOnly:=True, Password:="password"
End Sub

VBA Code:
Private Sub Workbook_Open()
  Dim Sht As Worksheet
 
  Set Sht = Sheets("MySheet")
 
  Sht.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, _
    AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowSorting:=True, _
    AllowFiltering:=True, AllowUsingPivotTables:=True, UserInterfaceOnly:=True, Password:="password"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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