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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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,833
Messages
6,121,858
Members
449,051
Latest member
excelquestion515

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