Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems Unprotecting Excel Worksheet 2

Status
Not open for further replies.

MikeBronner

Programmer
May 9, 2001
756
US
I'm trying to unprotect an excel worksheet using VB Code:
Sheet1.Unprotect
(The worksheet is already protected at this point.)

Excel returns the following error:
"Run-Time Error '1004':
Method 'Protect' of object '_WorkSheet' failed"

I also get this error if I try to protect the worksheet with the following code:
Sheet1.Protect(" ")

Is there a workaround that will let me protect and unprotect the worksheet using VB code?
Thanks! Take Care,
Mike
 
Try
ActiveWorkbook.Protect Password := "password"

and

ActiveSheet.Unprotect "password"
 
You can also do this without the sheet being the active sheet:
Code:
'With Password
Worksheets("Sheet1").Unprotect ("password")

'Without Password
Worksheets("Sheet1").Unprotect

 
I'm sorry to report that none of the abovementioned methods are working. I still get that error.

Here's the code I have:

Private Sub cmdAddInfo_Click()
Dim cText As String
Dim nCell As String

Worksheets("Pending").Unprotect
cText = InputBox("Insert your shift info into the following format:" & Chr(10) & "Shift - Date: Comments")
ActiveCell.Value = ActiveCell.Value & cText
nCell = ActiveCell.Address
Range("A3:L100").Select
Selection.RowHeight = 12.5
Range(nCell).Select
Worksheets("Pending").Protect
End Sub

Is there anything wrong in the code?

Thanks guys! :) Take Care,
Mike
 
I should also add that I don't have a password defined for that sheet. ;) Take Care,
Mike
 
I ran the entire thing again, and here's the problem:

When I run that code with the sheet unprotected at first, it will perform normaly and at the end of the sub it will protect it.

Now, when I run the sub again, it will produce the error message when trying to unprotect the sheet.

Any ideas? Take Care,
Mike
 
Finally, after trying around for some time I got it working with the final code like this:

Private Sub cmdAddInfo_Click()
' Macro recorded 8/31/2001 by Michael G. Bronner
Dim cText As String
Dim nCell As String

cText = InputBox("Insert your shift info into the following format:" & Chr(10) & "Shift - Date: Comments")
If ((cText <> &quot;&quot;) And (cText <> &quot; &quot;)) Then
nCell = ActiveCell.Address
Range(&quot;a3&quot;).Select
Worksheets(&quot;Pending&quot;).Unprotect Password:=&quot;SorterSheet&quot;
Range(nCell).Select
If ((ActiveCell.Value = &quot;&quot;) Or (ActiveCell.Value = &quot; &quot;)) Then
ActiveCell.Value = cText
Else
ActiveCell.Value = cText & vbCrLf & ActiveCell.Value
End If
nCell = ActiveCell.Address
Range(&quot;A3:L100&quot;).Select
Selection.RowHeight = 12.5
Range(&quot;A3&quot;).Select
Worksheets(&quot;Pending&quot;).Protect Password:=&quot;SorterSheet&quot;
Range(nCell).Select
End If
End Sub

Thanks for all your help guys! Take Care,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top