【问题标题】:"unable to set the size property of the font class" error in Excel VBA for conditional formatting, sheet is NOT protectedExcel VBA 中的“无法设置字体类的大小属性”错误,用于条件格式,工作表不受保护
【发布时间】: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) 运行没有问题。

这是错误的屏幕截图。如您所见,字体大小为11。 有人可以帮忙吗?

【问题讨论】:

  • 你传递的Range 是什么CellFormat?你确定.Font.Size 不是Null
  • @BigBen 我在上面更新了。感谢您的 cmets。

标签: excel vba


【解决方案1】:

简单的答案:您不能为条件格式设置字体名称或大小。这与VBA 无关,但与 Excel 相关:如果您使用常规 Excel 对话框为单元格设置条件格式,单击“格式”按钮并选择 Font-Tab,您会看到 FontSize 属性被禁用,你不能在那里选择任何东西:

找到了很好的解释here

条件格式不能做你想做的事,因为它只是为了改变外观格式,而不是真正改变单元格的属性。不同的字体有不同的样式和间距。假设是较大的字体会强制更改列的宽度或行的高度,这将是对工作表对象环境的更改,而条件格式无法做到这一点。如果你想改变字体样式,你需要自己做或使用 VBA

【讨论】:

    猜你喜欢
    • 2014-06-18
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2012-12-28
    • 2014-07-07
    相关资源
    最近更新 更多