【发布时间】:2020-05-06 20:11:36
【问题描述】:
我有一个像这样的长 VBA 宏:
Private Sub ApplyCondFormRun(CellFormat As Range, ValidFormula As String, TargetRange As Range, StopIfTrue As Boolean, Strict As Boolean)
'For this to work, cell addresses in validation formula must point to the first row
'of target cell
Dim ArrFormat(1 To 9) As Variant
Dim i As Long
'Application.ScreenUpdating = False
'attributes to be copied to destination cells
With CellFormat
ArrFormat(1) = .Font.Color 'Number
ArrFormat(2) = .Font.Size 'Number
ArrFormat(3) = .Font.Bold 'Boolean
ArrFormat(4) = .Font.Italic 'Boolean
ArrFormat(5) = .Font.Underline 'No: -4142, Single: 2, Double: -4119, Single Accounting: 4, Double Accounting: 5
If .Interior.ColorIndex = -4142 Then 'If cell is No fill then do nothing
ArrFormat(6) = .Interior.ColorIndex 'Number
Else
ArrFormat(6) = .Interior.Color
End If
ArrFormat(7) = .Borders(xlLeft).Color 'Number
ArrFormat(8) = .Borders(xlLeft).LineStyle 'Use only the left border style of the source cell & apply to whole destination cell
End With
ArrFormat(9) = StopIfTrue 'Boolean
TargetRange.FormatConditions.Add Type:=xlExpression, Formula1:=ValidFormula 'Add new cond formating
TargetRange.FormatConditions(TargetRange.FormatConditions.Count).SetFirstPriority
With TargetRange.FormatConditions(1)
.Font.Color = ArrFormat(1)
.Font.Size = ArrFormat(2)
....
代码在.Font.Size = ArrFormat(2) 行停止,出现“无法设置字体类的大小属性”错误。我研究了很多地方,包括here,但我的工作表根本没有受到保护。
仅供参考,CellFormat 范围是上图中所选范围的第一列。我会将这些单元格的格式应用于目标单元格的条件格式(第 3 列)。
此外,它上面的行 .Font.Color = ArrFormat(1) 运行没有问题。
【问题讨论】:
-
你传递的
Range是什么CellFormat?你确定.Font.Size不是Null。 -
@BigBen 我在上面更新了。感谢您的 cmets。