How to automatically copy from sheet to sheet, when sheets have to be protected?

09Tina

New Member
Joined
Dec 16, 2019
Messages
14
Office Version
  1. 365
Platform
  1. Windows
I'm interested in how to automatically copy from sheet to sheet, when sheets have to be protected. When i use the code written below in unprotected sheets it works perfectly. but when I use this code in combination

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ActiveWorkbook.Unprotect Password:="admin"
Dim k, LastRow
LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).row
For k = 2 To LastRow
    If Sheets("Sheet1").Cells(k, "G").Value = 0 Then
    Sheets("Sheet1").Cells(k, "G").EntireRow.Cut Destination:=Sheets("Archive").Range("A" & Rows.Count).End(xlUp).Offset(1)
    Sheets("Sheet1").Cells(k, "G").EntireRow.Delete
    End If
Next k
ActiveWorkbook.Protect Password:="admin"
End Sub

But when I use this code in combination with ProtectCell code I get Error 1004 that says that I have to delete a DeleteEntireRow line in the code, but when I delete it, an error 1004 is shown again and says that I can't work in a protected sheet.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cl As Range
ActiveSheet.Unprotect Password:="pass"
For Each cl In Target
    If cl.Value <> "" Then
    cl.Locked = True
    End If
Next cl
ActiveSheet.Protect Password:="admin"
End Sub
Please help!
 
glad we helped
- but I am surprised that deleting Worksheet_Change was your solution :unsure:
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
glad we helped
- but I am surprised that deleting Worksheet_Change was your solution :unsure:

I'm not, I forgot to include Application.EnableEvents = False in my code to prevent that event triggering - but no worries, we both helped resolve new OPs issue which is a good result

Dave
 
Upvote 0
You were right, it was too changed and complicated for exel recognize that the sheet was unprotected in line before, because of code Worksheet_Change wich worked with locked and unlocked cells andprotected and unprotected sheets, i think. And yes, You helped me a lot guys, thank You very much.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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