Excel) (Range.Text 屬性

傳回指定物件的格式化文字。 唯讀的 String。

語法

運算式。文字

expression 代表 Range 物件的變數。

註解

Text 屬性最常用於一個儲存格的範圍。 如果範圍包含多個儲存格,則 Text 屬性會傳回 Null,除非範圍中的所有儲存格具有相同的內容和格式。

如果儲存格內容的寬度大於可供顯示的寬度, Text 屬性會修改顯示的值。

屬性差異範例

此範例說明含有格式化數字之儲存格的 Text 和 Value 屬性之間的差異。

Option Explicit

Public Sub DifferencesBetweenValueAndTextProperties()
    Dim cell As Range
    Set cell = Worksheets("Sheet1").Range("A1")
    cell.Value = 1198.3
    cell.NumberFormat = "$#,##0_);($#,##0)"
    
    MsgBox "'" & cell.Value & "' is the value." 'Returns: "'1198.3' is the value."
    MsgBox "'" & cell.Text & "' is the text."    'Returns: "'$1,198' is the text."
End Sub

文字寬度差異

當欄不夠寬時,含有數值的儲存格可能會修改其顯示值。 下面的範例使用兩個欄來顯示這一點。 第一欄的寬度足以顯示值。 系統會套用格式,然後輸入顯示完整值的值。 第二欄的寬度已縮減,當將儲存格複製到上方時,欄太窄,導致顯示的值變得微調。

Public Sub TextWidthDifferences()
    
    Dim wideColumn As Range
    Set wideColumn = Sheet1.Range("B2")
    wideColumn.Value = "Wide Enough Column"
    wideColumn.Columns.AutoFit
    
    Sheet1.Range("B3").Value2 = 123456789
    
    Const CurrencyWith2DecimalsFormat As String = "$#,##0.00"
    Dim currencyCell As Range
    Set currencyCell = Sheet1.Range("B4")
    currencyCell.Value2 = 1234.56
    currencyCell.NumberFormat = CurrencyWith2DecimalsFormat
    
    Dim narrowColumn As Range
    Set narrowColumn = Sheet1.Range("C2")
    narrowColumn.Value = "Reduced Width Column"
    narrowColumn.ColumnWidth = 7.5
    
    Sheet1.Range("B3:B4").AutoFill Destination:=Sheet1.Range("B3:C4"), Type:=XlAutoFillType.xlFillDefault
    Debug.Print Sheet1.Range("C3").Text
    Debug.Print Sheet1.Range("C4").Text
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。