Visio) (Application.FormatResult 方法

根據格式圖片,將字串或數位格式化為字串。 使用指定的單位進行調整和格式化。

語法

運算式FormatResult (StringOrNumberUnitsInUnitsOutFormat)

expression 代表 Application 物件的變數。

參數

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

傳回值

字串

註解

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

StringOrNumber 可能的值包括:

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

UnitsInUnitsOut 自變數可以是字串,例如 「inches」、「inch」、「in.」 或 「i」。 字串可用於所有支援的 Microsoft Office Visio 單位,例如公分、公尺、英哩等等。 您也可以使用 Visio 型別程式庫在 VisUnitCodes中宣告的任何單位常數。 [ 關於量值單位] 中也會包含有效單位的清單。

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