共用方式為


Excel) (Range.Text 屬性

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

語法

運算式文本

expression 代表 Range 物件的變數。

註解

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

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

屬性差異範例

此範例說明包含格式化數位之儲存格的 TextValue 屬性之間的差異。

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