尝试更改背景和字体颜色时应用程序或对象定义的错误

我试图replace电子表格的所有条件格式(超过30格式化规则)。 我创build了一个类模块(称为ConditionalFormatting),它对所有的格式化规则都有一系列的子类,每个范围都有一个需要条件格式的子类。 唯一的方法,我想这样做(开放的build议)是通过使工作表变化事件调用ConditionalFormatting类中的一个名为FormattingSubs的子将调用正确的子执行格式。

这里是FormattingSubs的代码:

Public Sub FormattingSubs(target As Range) 'have logic here to call the right sub based on what target.address 'is from the worksheet.change event Select Case target.Name.Name Case "head_pouch_lot_number" Call HeadPouchLotNumber(target) Case "head_consumed_pouch_lot" Call HeadConsumedPouchLot(target) Case "section_one_heading" Call SectionOneHeading End Select End Sub 

这里是一个格式化子代码HeadConsumedPouchLot:(注意颜色variables是在一个单独的模块中定义的公共常量)

 Public Sub HeadConsumedPouchLot(target As Range) Dim head_consumed_pouch_lot As Range Dim ws As Worksheet Set head_consumed_pouch_lot = ActiveSheet.Range("head_consumed_pouch_lot") Set ws = target.Worksheet If target.address <> head_consumed_pouch_lot.address Then Set target = head_consumed_pouch_lot End If With ws.Range(target.address) If Range("section_one_heading").Value <> "" Then .Interior.ColorIndex = red .Font.ColorIndex = yellow Else .Interior.ColorIndex = lightgreen .Font.ColorIndex = black End If End With 

问题是,当它真的设置颜色,它给了我1004错误:“应用程序定义或对象定义的错误。

我的代码有什么问题?

我发现的问题是,我需要取消保护我的工作表,然后才能对其进行任何更改! 谢谢大家帮助我寻找答案。