run-time error '1004' when sheet is protected

cybrkada

New Member
Joined
Apr 8, 2014
Messages
42
Can you tell me what's wrong with my codes? I have macros on my other protected sheets and they are working fine. I have summary sheet named Index positioned at the very last and that's where I am getting the error whenever I protect it. Here are the codes:

Code:
Private Sub Worksheet_Activate()
Dim wSheet As Worksheet
Dim n As Integer
Dim calcState As Long, scrUpdateState As Long


calcState = Application.Calculation
Application.Calculation = xlCalculationManual
scrUpdateState = Application.ScreenUpdating
Application.ScreenUpdating = False


n = 1


With Me
.Columns(1).ClearContents
.Cells(1, 1) = "Date"
.Cells(1, 1).Name = "Index"
End With

For Each wSheet In Worksheets
If wSheet.Name <> Me.Name Then
n = n + 1
With wSheet
.Range("B1").Name = "Start_" & wSheet.Index
.Hyperlinks.Add anchor:=.Range("B1"), Address:="", _
SubAddress:="Index", TextToDisplay:="Back to Index"
End With

Me.Hyperlinks.Add anchor:=Me.Cells(n, 1), Address:="", _
SubAddress:="Start_" & wSheet.Index, TextToDisplay:=wSheet.Name
End If
Next wSheet

Application.Calculation = calcState
Application.ScreenUpdating = scrUpdateState
End Sub

Whenever I debug the codes, the line .Columns(1).ClearContents is being highlighted. Did I miss anything?

Thanks,
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
If contents of your worksheet are protected you need to unprotect them before carrying the actions.

Try adding this change & see if helps. you will need to insert password if required where shown.

Rich (BB code):
With Me
        .Unprotect Password:="passwordhere"
        .Columns(1).ClearContents
        .Cells(1, 1) = "Date"
        .Cells(1, 1).Name = "Index"
    End With

Dave
 
Upvote 0
Hi Dave,

Thanks for your reply but I don't want to unprotect the sheet, the reason I am doing it is to prevent users from accidentally deleting cells and seeing the formulas. However I am getting the runtime error everytime the sheet gets protected.

Thanks,
 
Upvote 0
Hi Dave,

Thanks for your reply but I don't want to unprotect the sheet, the reason I am doing it is to prevent users from accidentally deleting cells and seeing the formulas. However I am getting the runtime error everytime the sheet gets protected.

Thanks,

Just re-protect once code action is complete:

Rich (BB code):
With Me
        .Unprotect Password:="passwordhere"
        .Columns(1).ClearContents
        .Cells(1, 1) = "Date"
        .Cells(1, 1).Name = "Index"
        .Protect Password:="passwordhere"
    End With
 
Upvote 0
Code:
Private Sub Worksheet_Activate()
Dim wSheet As Worksheet
Dim n As Integer
Dim calcState As Long, scrUpdateState As Long

calcState = Application.Calculation
Application.Calculation = xlCalculationManual
scrUpdateState = Application.ScreenUpdating
Application.ScreenUpdating = False

n = 1

With Me
.Unprotect Password:="Secret"
.Columns(1).ClearContents
.Cells(1, 1) = "Date"
.Cells(1, 1).Name = "Index"
.Protect Password:="Secret"
End With
For Each wSheet In Worksheets
If wSheet.Name <> Me.Name Then
n = n + 1
With wSheet
.Range("B1").Name = "Start_" & wSheet.Index
.Hyperlinks.Add anchor:=.Range("B1"), Address:="", _
SubAddress:="Index", TextToDisplay:="Back to Index"
End With
Me.Hyperlinks.Add anchor:=Me.Cells(n, 1), Address:="", _
SubAddress:="Start_" & wSheet.Index, TextToDisplay:=wSheet.Name
End If
Next wSheet
[COLOR=#0000ff][B]Application.Calculation = calcState
[/B][/COLOR]Application.ScreenUpdating = scrUpdateState
End Sub

Hi, I am still getting the same error but it is highlighting a different line when I debug it! Also the macro does not work anymore. Please help!
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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