VBA Activesheet.protect "password" Error '1004'

malezlotko

New Member
Joined
Jun 24, 2010
Messages
3
I am building a database and would like to password protect certain cells. I have been using both of the below successfully:

activesheet.unprotect "password"
code...
activesheet.protect "password"

or

Worksheets("sheetname").Unprotect Password:="password"
code...
Worksheets("sheetname").Protect Password:="password"

However, once I try to apply the above to the code below, it returns run-time error '1004' application defined or object defined error.

How come the code below is not accepting? If I remove the re-protect command it runs and does what it needs to do but the issue is it does not re-protect the cells.

Thanks,

_______________________________________________________________


Private Sub worksheet_change(ByVal target As Range)
Worksheets("report").Unprotect Password:="Password"
If target.Cells.Count = 1 Then
If target.Value = "Go" Then
target.Offset(0, 11) = Now
target.Offset(0, -1) = "In Progress"
target.Offset(0, -2).Value = Application.WorksheetFunction.VLookup(target.Offset(0, -2).Value, Worksheets("LookupLists").Range("c24:d31"), 2, 0)
target.Offset(0, 11).Select
Selection.Cut
Selection.End(xlToLeft).Select
ActiveSheet.Paste
target.EntireRow.Copy
Sheets("Update History").Range("a60000").End(xlUp).Offset(1, 0).Insert
target.Value = " "
End If
If target.Value = "Retired - No-Go" Or target.Value = "Dropped by AM" Then
target.Offset(0, 12) = Now
target.Offset(0, 1) = "No-Go"
target.Offset(0, 12).Select
Selection.Cut
Selection.End(xlToLeft).Select
ActiveSheet.Paste
target.EntireRow.Copy
Sheets("Retired").Range("a60000").End(xlUp).Offset(1, 0).Insert
target.EntireRow.Delete
End If
End If
Worksheets("report").Protect Password:="password"
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
If you password protect a sheet and set the argument UserInterFaceOnly:=True, you will protect the sheet but macros will be able to manipulate the sheet without the need to unprotect it.

Code:
Worksheets("sheetname").Protect Password:="password", UserInterFaceOnly:=True

Caveat: you need to set this argument every time you open the workbook. Read this...
http://www.ozgrid.com/VBA/excel-macro-protected-sheet.htm
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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