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 支援與意見反應