Where to protect sheet in this code?

MeaclH

Board Regular
Joined
Apr 2, 2014
Messages
63
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
G'day all,
Just wondering where to put the code to protect the sheet in this code?
Many thanks all!
Hayden

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Resp As String

If Target.CountLarge = 1 And Not Intersect(Target, Range("G10:T172")) Is Nothing Then
ActiveSheet.Unprotect
    With Target
      Select Case .Value
        Case "SDO", "STFN", "CDO", "CTFN" '<- Add more trigger values here if required
          Resp = Application.InputBox("Please insert details of absenteeism", _
          Title:="Absenteeism Details")
          If Len(Resp) > 0 And Resp <> "False" Then
            If Not .Comment Is Nothing Then
              .Comment.Text .Comment.Text & vbLf & Resp
            Else
              .AddComment Text:=Resp
            End If
          End If
      End Select
    End With
  End If
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Assuming it was protected to begin with, I'd say between End With and the End If that follows that. That will take care of the situation where the outer if is True for both parts AND the Case is one of those cases. Also, if the outer if is True but the Case is not True, code exits the Case block but will still encounter the protection line if placed as suggested. The only situation where the protection will not be removed is if the outer If evaluates to False.

If you put the protection before End Sub, then you'll be protecting the sheet even if it is already protected. I suppose that would not be an issue - it's just not real efficient in terms of writing code.
 
Upvote 0

Forum statistics

Threads
1,221,534
Messages
6,160,385
Members
451,645
Latest member
hglymph

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