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 可在從 Visual Basic for Applications (VBA) 呼叫的函數中使用。 例如,在下列 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 支援與意見反應