Error with Protecting Sheets

craigwojo

Board Regular
Joined
Jan 7, 2005
Messages
218
Hi everyone,

Whenever I protect the sheet that has the following code in it and when I try to access the cell it gives me:
Run-time error '1004':
Unable to set the ColorIndex property of the Interior class

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("C3:C502")) Is Nothing Then Exit Sub
Dim bcolor As Integer, fcolor As Integer
Select Case Target
Case "Black Arrows on Green"
bcolor = 50: fcolor = 1
Case "White Arrows on Green"
bcolor = 50: fcolor = 2
Case "White Arrows on Red"
bcolor = 3: fcolor = 2
Case "Black Arrows on Yellow"
bcolor = 6: fcolor = 1
Case "White Arrows on Blue"
bcolor = 32: fcolor = 2
End Select
With Target
.Interior.ColorIndex = bcolor
.Font.ColorIndex = fcolor
.Font.Bold = True
End With
End Sub


When I unlock the sheet it works fine.

I select "Select unlocked cells" for allowing the users to use. The drop-down menu on the cell that the code uses is UNLOCKED (in the protection selction of format cells.

Why does this happen and can it be corrected?

Thank you,
Craig
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,253
Office Version
  1. 2016
Platform
  1. Windows
See if this works : Edit the code as follows:

Code:
Me.Unprotect  '\\ temporarly unprotect sheet
With Target
.Interior.ColorIndex = bcolor
.Font.ColorIndex = fcolor
.Font.Bold = True
End With
Me.Protect '\\ restore protection

Regards.
 

Bartek

Board Regular
Joined
Jul 29, 2006
Messages
54
Hi,

Whenever I protect the sheet that has the following code in it and when I try to access the cell it gives me:
Run-time error '1004':
Unable to set the ColorIndex property of the Interior class

If you are using Excel 2002 or latter you may protect the worksheet with the option AllowFormattingCells:=True - this will solve your problem. AllowFormattingCells will not work in Excel 2000 - in Excel 2000 you may change the font.colorindex of unlocked cells, but not interior.colorindex. In that case, you have to temporarily unprotect the worksheets, as described above.
 

craigwojo

Board Regular
Joined
Jan 7, 2005
Messages
218
Jaafar,

Where do I put the code? Do I insert it within the code?

Thank you,
Craig


See if this works : Edit the code as follows:

Code:
Me.Unprotect  '\\ temporarly unprotect sheet
With Target
.Interior.ColorIndex = bcolor
.Font.ColorIndex = fcolor
.Font.Bold = True
End With
Me.Protect '\\ restore protection

Regards.
 

Forum statistics

Threads
1,136,594
Messages
5,676,700
Members
419,644
Latest member
KeelsM

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
Top