As mentioned in Comments by @TnTinMn, the repeated need to call Unprotect would be an action occurring in a Workbook Event.
If possible, you can update the code within the necessary events to prevent the Protect command from executing. This may not be desirable as the code was potentially added to ensure the sheets remain protected.
Some suggests on cleaning the original code snippet up a bit to allow for easier maintenance.
Each of the two worksheets you are referencing are assigned to variables. Despite having the variable setup, all the Unprotect calls spell out the full name instead of using the variable.
This:
Sheets("CDRF File (1)").Unprotect Password:="hammer"
could be:
wsCopyTo.Unprotect Password:="hammer"
The password is specified in multiple location. Save it to a variable so there is only one update necessary on a password change.
'add to declaration section
dim pwStr as string
pwStr = "hammer"
'...
wsCopyTo.Unprotect Password:=pwStr
The initial Unprotect should be executed after the assignment, which allows use of the worksheet variable.
Set wsCopyFrom = ActiveWorkbook.Worksheets("STP_Data")
Set wsCopyTo = ActiveWorkbook.Worksheets("CDRF File (1)")
'unprotect sheets for CDRF (1)
wsCopyTo.Unprotect Password:=pwStr
Comments should be commented. Maybe these were added for clarity after copying to the post, but as is it won't compile.
'Entity Name
'CIF
The suggestion for With...End With statement was a suggestion to further clean up the appearance. Instead of continually stating wsCopyTo. , wrap the block in With wsCopyTo
Then instead of:
wsCopyTo.Range(
The line can start with .
.Range(
With wsCopyTo
'unprotect sheets for CDRF (1)
.Unprotect Password:="hammer"
'Entity Name
.Range("B11") = wsCopyFrom.Range("E30")
end with
Updated code for the snippet you provided
Set wsCopyFrom = ActiveWorkbook.Worksheets("STP_Data")
Set wsCopyTo = ActiveWorkbook.Worksheets("CDRF File (1)")
With wsCopyTo
'unprotect sheets for CDRF (1)
.Unprotect Password:=pwStr
'Entity Name
.Range("B11") = wsCopyFrom.Range("E30")
'unprotect sheets for CDRF (1)
.Unprotect Password:=pwStr
'CIF
.Range("K11") = wsCopyFrom.Range("F30")
'unprotect sheets for CDRF (1)
.Unprotect Password:=pwStr
'TIN
.Range("M11") = wsCopyFrom.Range("G30")
End With
With...End With Statement- 0m3r