Excel) (Range.DisplayFormat 属性

返回表示指定范围的显示设置的 DisplayFormat 对象。 此为只读属性。

语法

表达式DisplayFormat

expression 一个表示 Range 对象的变量。

返回值

DisplayFormat

备注

DisplayFormat 受条件格式的影响,如以下代码所示。 它将条件格式添加到 ActiveSheet 上的单元格 A1。 此格式会使单元格加粗,将内部颜色更改为红色,并添加方格图案。

Public Sub DemonstrateConditionalFormattingAffectsDisplayFormat()
    Dim inputArea As Range
    Set inputArea = ActiveSheet.Range("A1")
    
    Dim addedFormatCondition As FormatCondition
    Set addedFormatCondition = inputArea.FormatConditions.Add(xlExpression, Formula1:="=true")
    addedFormatCondition.Font.Bold = True
    addedFormatCondition.Interior.Color = XlRgbColor.rgbRed
    addedFormatCondition.Interior.Pattern = XlPattern.xlPatternChecker
    
    Debug.Print inputArea.Font.Bold 'False
    Debug.Print inputArea.Interior.Color 'XlRgbColor.rgbWhite
    Debug.Print inputArea.Interior.Pattern 'XlPattern.xlPatternNone
    
    Debug.Print inputArea.DisplayFormat.Font.Bold 'True
    Debug.Print inputArea.DisplayFormat.Interior.Color 'XlRgbColor.rgbRed
    Debug.Print inputArea.DisplayFormat.Interior.Pattern 'XlPattern.xlPatternChecker
End Sub

请注意, DisplayFormat 属性在用户定义的函数 (UDF) 中不起作用。 例如,在返回单元格内部颜色的工作表函数上,使用类似于以下的行。 Range(n).DisplayFormat.Interior.ColorIndex 当工作表函数执行时,它返回 #VALUE! 错误。

在其他示例中,不能使用工作表函数中的 DisplayFormat 属性返回特定区域的设置。 但是,DisplayFormat 将适用于从 VBA) 调用Visual Basic for Applications (函数。 例如,在以下 UDF 中:

Function getDisplayedColorIndex()
   getColorIndex = ActiveCell.DisplayFormat.Interior.ColorIndex
End Function

按如下方式从工作表调用函数 =getDisplayedColorIndex () 返回 #VALUE! 错误。 因此,如果将条件格式应用于某个区域,则无法使用 UDF 返回该值。 如果已应用条件格式,请通过调用 Visual Basic 编辑器中的“即时”窗格来获取活动单元格的颜色索引。

如果未应用条件格式,请使用以下函数返回活动单元格的颜色索引。 以下函数将从工作表或 VBA 工作。

Function getAppliedColorIndex()
   getColorIndex = ActiveCell.Interior.ColorIndex
End Function

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。