Runtime error 1004 Unable to set locked property

grahamiwa

New Member
Joined
Mar 14, 2011
Messages
30
I have a sub that I have copied out of a previous workbook where it still works fine. In the previous workbook it was assigned to a button. Now I am trying just to do it as an automatic action when the workbook is closed

It will lock any cell within the specified range that is not blank then protect the worksheet, save the workbook then save a dated archive copy

When I pasted it in to a new workbook I changed the range to A1:AP49 and strFileName

Now when I run it now I get an Error 1004 - Unable to set the Locked property of the Range class.

On Debug this line is highlighted:-
c.Locked = c.Value <> ""



The full code is:-

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'
' Lock_cells Macro
' Macro compiled 10/26/2011 by G CARNCROSS
' Locks used cells, saves the workbook then saves a password protected copy with the days date in the file name then closes the workbook
'
'
ActiveSheet.Unprotect Password:="SHES"

Dim c As Range
For Each c In Range("A1:AP49")
c.Locked = c.Value <> ""
Next c

Application.DisplayAlerts = False
ActiveSheet.Protect Password:="SHES", DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save

Dim strFileName As String

strFileName = "\\mlbdat02\shared\organization\T&D-All\Training Course Archive \REP Training Schedule" & Chr(32) & Format(Date, "d mmm yyyy") & ".xls"

ActiveWorkbook.SaveAs Filename:=strFileName, FileFormat:= _
xlNormal, Password:="SHES", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False

Application.DisplayAlerts = True

ActiveWorkbook.Close



End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Great

I did have merged cells in the range which I an not able to change but I have readjusted the renge and it works well now

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,969
Messages
6,182,077
Members
453,088
Latest member
Chaoxite

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