共用方式為


Application.FormatResult method (Visio)

根據格式圖片將字串或數字格式化成字串。 使用指定單位進行縮放與格式化。

語法

表情FormatResult (StringOrNumberUnitsInUnitsOutFormat)

expression 代表 Application 物件的變數。

參數

名稱 必要/選用 資料類型 描述
弦或編號 必要 Variant 要格式化的字串或數字;可以傳遞為字串、浮點數或整數。
單位 必要 Variant 歸類至 StringOrNumber 所用的度量單位。
單位退役 必要 Variant 表示結果所用的度量單位。
Format 必要 字串 結果字串外觀的圖片。

傳回值

字串

註解

如果是以字串的形式傳遞,則 StringOrNumber 可能是儲存格的公式或預期的公式,或是以字串表示之儲存格的結果或預期結果。 FormatResult 方法會評估此字串,並格式化所產生的結果。 由於此字串是在特定儲存格的公式內容之外進行評估,所以如果此字串有包含任何儲存格參考,則 FormatResult 方法會傳回錯誤。

StringOrNumber 可能的值包括:

  • 1.7
  • 3
  • "2.5"
  • "4.1 cm"
  • "12 ft - 17 in. + (12 cm / SQRT(7))"

UnitsInUnitsOut 參數可以是字串,如「英吋」、「英吋」、「內」或「i」。 字串可用於所有支援的 Microsoft Office Visio 單位,如公分、公尺、英里等。 你也可以使用 VisUnitCodes 中 Visio 類型庫中宣告的任何單位常數。 有效單位列表亦包含於 「關於度量單位」中。

如果 StringOrNumber 是一個字串, UnitsIn 會指定如何解讀評估結果,且僅在結果為純量時使用。 例如,該表達 "4 * 5 cm" 式的值值為 20 公分,這不是純量,因此忽略 了 UnitsIn 。 該表達 "4 * 5" 式的值值為 20,這是一個純量,並透過指定的 UnitsIn 來解釋。

UnitsOut 參數指定回傳字串應以何種單位表達。 如果你想讓結果用與評估表達式相同的單位表示,請通過「NOCAST」或 visNoCast

Format 是一個字串,會指定由 FormatResult 方法所產生的範本或字串的圖片。 如需詳細資訊,請參閱 FORMAT 函數。 以下列出一些可能的情況:

  • # - 輸出單位數字,但若為前導或後尾 0 則不輸出。

  • 0 - 輸出單位數字,即使該數字是前置或後尾的 0。

  • . - 小數佔位符。

  • , - 千分隔。

  • "text"'text' - 原封面文字輸出。

  • \c - 輸出字元 c。

範例

其中指定了一串字串。

' Prints 1.00 
Debug.Print Application.FormatResult("0.5 * 2", "ft", "ft", "#.00 u") 
 
' Prints 12.00 in. 
Debug.Print Application.FormatResult("0.5 * 2", "ft", "in", "#.00 u") 
 
' Prints .39 in. 
Debug.Print Application.FormatResult("1 cm", "ft", "in", "#.00 u") 
 
' Prints 1.00 cm. 
Debug.Print Application.FormatResult("1 cm", "ft", "NOCAST", "#.00 u") 
 
' Prints 0.39 
Debug.Print Application.FormatResult("1 cm", "ft", "", "0.00 u") 
 
' Prints 1858.06 sq. cm. 
Debug.Print Application.FormatResult("1 sq. ft. * 2", "in^2", "cm^2", "0.00 u") 
 
' Throws an exception because of bad measurement unit ("bz") 
Debug.Print Application.FormatResult("1 cm", "ft", "bz", "#.00 u") 

其中指定了一個數字。

' Prints 1.00 
Debug.Print Application.FormatResult(1, "ft", "ft", "#.00 u") 
 
' Prints 12.00 in. 
Debug.Print Application.FormatResult(1, "ft", "in", "#.00 u") 
 
' Prints .08 ft. 
Debug.Print Application.FormatResult(1.0, "in", "ft", "#.00 u") 
 
' Prints 12.00 
Debug.Print Application.FormatResult(1.0, visFeet, "", "#.00 u") 
 
' Throws an exception because of bad measurement unit ("bz") 
Debug.Print Application.FormatResult(1, "bz", "in", "#.00 u") 


下列巨集會示範如何使用 FormatResult 方法,將公分的值轉換為英吋,並將結果顯示在訊息方塊中。

 
Public Sub FormatResult_Example() 
 
 Dim strOldValue As String 
 Dim strNewValue As String 
 
 'Set old value. 
 strOldValue = "1 cm" 
 
 'Format value. 
 strNewValue = Application.FormatResult _ 
 (strOldValue, "ft", "in", "#.00 u") 
 
 'Display new value. 
 MsgBox (strNewValue) 
 
End Sub

支援和意見反應

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