Runtime Error 1004

mikemmb

Board Regular
Joined
Mar 4, 2009
Messages
59
Hi,
I need help again, but I am learning a lot from you guys!

I have just applied protection to certain cells within a worksheet.
The required cells are protected and others are not.

However in some unprotected cells I have a sub to toggle "ticks" in the box see below:
"Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("B7:D1000")) Is Nothing Then
Target.Font.Name = "Marlett"
If Target = vbNullString Then
Target = "a"
Else
Target = vbNullString
End If
End If
End Sub"

Whenever I click on one of these cells, the "ticks" do not toggle and I get the following error:
"Runtime error 1004
Unable to set the name property of the font class".


If I Unprotect the sheet it works fine.

Clearly the protection function is screwing up the font change in the sub, but I am stuck on why or how as I thought that I had strictly limited the protection to just specific cells. I have found some references to this error but no real conclusion as to the best solution?

Thanks,
Mike
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Can you just unprotect the sheet before changing the name and then protecting it again? Is it protedted with a password, if not try this:

Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("B7:D1000")) Is Nothing Then
activesheet.unprotect
Target.Font.Name = "Marlett"
activesheet.protect
If Target = vbNullString Then
Target = "a"
Else
Target = vbNullString
End If
End If
End Sub
Otherwise if it has a password you will have to put that after the protect in quotes.

Hope that helps.
 
Upvote 0
Hi Mike:

I think you just need to Add Unprotect and Protect to your code. Try this:


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
 
ActiveSheet.Unprotect
'ActiveSheet.Unprotect "password" 'IF password protected use this line instead
 
If Not Intersect(Target, Range("B7:D1000")) Is Nothing Then
Target.Font.Name = "Marlett"
If Target = vbNullString Then
Target = "a"
Else
Target = vbNullString
End If
End If
 
ActiveSheet.Protect
ActiveSheet.Protect "password" 'IF password protected use this line instead
 
 
End Sub

Good Luck,
Mark :)
 
Upvote 0
Thanks all, it was cured by putting unprotect/protect in the sub as suggested.
Thanks and Regards,
Mike
 
Upvote 0
Hi,
In use the Unprotect/Protect solution works perfectly technically, but it does cause problems because regardless of the protection status prior to running the sub, it always ends up applying protection.

Is there a way of making the "protect" code conditional on the original status.
Something along the lines of: if it was protected before then re-protect it, if not do not protect it.

Regards,
Mike
 
Upvote 0
Try this:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim IsProtected As Boolean
If Target.Cells.Count > 1 Then Exit Sub
IsProtected = Me.ProtectContents = True
If IsProtected Then Me.Unprotect
If Not Intersect(Target, Range("B7:D1000")) Is Nothing Then
    Target.Font.Name = "Marlett"
    If Target = vbNullString Then
        Target = "a"
    Else
        Target = vbNullString
    End If
End If
If IsProtected Then Me.Protect
End Sub
 
Upvote 0
Thanks VoG,
Will not be able to try this for a couple of days, so will not be able to respond, but where does my password (used in Mister H's post of the thread) come into this? do I swap Me for "mypassword"?

Thanks,
Mike
 
Upvote 0
No! Me refers to the sheet in which the code resides. Try

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim IsProtected As Boolean
If Target.Cells.Count > 1 Then Exit Sub
IsProtected = Me.ProtectContents = True
If IsProtected Then Me.Unprotect Password:="mypassword"
If Not Intersect(Target, Range("B7:D1000")) Is Nothing Then
    Target.Font.Name = "Marlett"
    If Target = vbNullString Then
        Target = "a"
    Else
        Target = vbNullString
    End If
End If
If IsProtected Then Me.Protect Password:="mypassword"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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