尝试更改背景和字体颜色时出现应用程序或对象定义错误
Application or object defined error when trying to change background and font colors
我正在尝试替换电子表格的所有条件格式(超过 30 条格式规则)。我创建了一个 class 模块(称为 ConditionalFormatting),其中包含一系列用于所有格式设置规则的子项,每个需要条件格式设置的范围都有一个子项。
我想到这样做的唯一方法(接受建议)是让 worksheet_change 事件在 ConditionalFormatting class 中调用一个名为 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 的代码:(请注意,颜色变量是 public 在单独模块中定义的常量)
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 错误:"Application-defined or object-defined error."
我的代码有什么问题?
我发现的问题是我需要取消保护我的 sheet 才能对其进行任何更改!谢谢大家帮我寻找答案。
我正在尝试替换电子表格的所有条件格式(超过 30 条格式规则)。我创建了一个 class 模块(称为 ConditionalFormatting),其中包含一系列用于所有格式设置规则的子项,每个需要条件格式设置的范围都有一个子项。 我想到这样做的唯一方法(接受建议)是让 worksheet_change 事件在 ConditionalFormatting class 中调用一个名为 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 的代码:(请注意,颜色变量是 public 在单独模块中定义的常量)
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 错误:"Application-defined or object-defined error."
我的代码有什么问题?
我发现的问题是我需要取消保护我的 sheet 才能对其进行任何更改!谢谢大家帮我寻找答案。