&引用;无法设置字体类“的大小”属性;Excel VBA中的条件格式错误,工作表不受保护
我有一个长VBA宏,如下所示:&引用;无法设置字体类“的大小”属性;Excel VBA中的条件格式错误,工作表不受保护,excel,vba,Excel,Vba,我有一个长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 c
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)
行停止。我研究了很多地方,包括,但我的床单根本没有保护。
仅供参考,范围CellFormat是上图中选定范围的第一列。我将这些单元格的格式应用于目标单元格的条件格式(第3列)
此外,它上面的行.Font.Color=ArrFormat(1)
,运行时没有问题
这是错误的屏幕截图。正如你所看到的,字体大小是11。
有人能帮忙吗?简单回答:您不能为条件格式设置字体名称或大小。这与
VBA
无关,但与Excel有关:如果使用常规Excel对话框为单元格设置条件格式,请单击“格式”按钮并选择Font
-选项卡,您会看到Font
和Size
属性被禁用,您无法选择其中的任何内容:
找到了一个很好的解释:
条件格式无法实现您想要的功能,因为它仅用于更改外观格式,而不是真正更改单元格的属性。不同的字体有不同的样式和间距。假定较大的字体将强制更改列的宽度或行的高度,这将是对工作表对象环境的更改,而条件格式无法做到这一点。如果你想改变字体样式,你需要自己做或者使用VBA
简单回答:不能为条件格式设置字体名称或大小。这与
VBA
无关,但与Excel有关:如果使用常规Excel对话框为单元格设置条件格式,请单击“格式”按钮并选择Font
-选项卡,您会看到Font
和Size
属性被禁用,您无法选择其中的任何内容:
找到了一个很好的解释:
条件格式无法实现您想要的功能,因为它仅用于更改外观格式,而不是真正更改单元格的属性。不同的字体有不同的样式和间距。假定较大的字体将强制更改列的宽度或行的高度,这将是对工作表对象环境的更改,而条件格式无法做到这一点。如果你想改变字体样式,你需要自己做或者使用VBA
您作为
CellFormat
传递的Range
是什么?你确定.Font.Size
不是Null
@BigBen I上面更新的吗。Tks用于您的评论。您作为CellFormat
传递的范围是什么?你确定.Font.Size
不是Null
@BigBen I上面更新的吗。谢谢你的评论。