ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Why Can't Unprotect and write value into worksheet before close. (https://www.excelbanter.com/excel-programming/282957-why-cant-unprotect-write-value-into-worksheet-before-close.html)

EdgeOfCity

Why Can't Unprotect and write value into worksheet before close.
 
worksheets("sheet2") has been protected from "Tool" menu with password
"test".
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim i As Integer
With Worksheets("sheet2")
.Activate
.Unprotect Password:="test"
On Error Resume Next
.Cells(4, 4) = "HI"
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True,
Password:="test"
End With
ActiveWorkbook.Close SaveChanges:=True
End Sub

the question is that why should add "On error resume next"?



Bob Umlas[_3_]

Why Can't Unprotect and write value into worksheet before close.
 
You have a close statement inside your before_close event. That's a no-no.
It's about to close anyway. Single step thru the close event and watch it
run more than once.
If you remove the Close statement and replace with a Save, you can also
remove the On Error Resume Next.
Bob Umlas
Excel MVP

"EdgeOfCity" wrote in message
...
worksheets("sheet2") has been protected from "Tool" menu with password
"test".
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim i As Integer
With Worksheets("sheet2")
.Activate
.Unprotect Password:="test"
On Error Resume Next
.Cells(4, 4) = "HI"
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True,
Password:="test"
End With
ActiveWorkbook.Close SaveChanges:=True
End Sub

the question is that why should add "On error resume next"?





Tom Ogilvy

Why Can't Unprotect and write value into worksheet before close.
 
I don't see any reason to do it except that you are closing the workbook in
the beforeclose event which would trigger another beforeclose event and
possibly so forth.

--
Regards,
Tom Ogilvy



"EdgeOfCity" wrote in message
...
worksheets("sheet2") has been protected from "Tool" menu with password
"test".
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim i As Integer
With Worksheets("sheet2")
.Activate
.Unprotect Password:="test"
On Error Resume Next
.Cells(4, 4) = "HI"
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True,
Password:="test"
End With
ActiveWorkbook.Close SaveChanges:=True
End Sub

the question is that why should add "On error resume next"?






All times are GMT +1. The time now is 01:31 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com