共用方式為


Range.DisplayFormat 屬性 (Excel)

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