根據圖片格式將字串或數字格式化為字串。 使用指定的單位進行縮放和格式設定。
語法
運算式。FormatResult (StringOrNumber, UnitsIn, UnitsOut, Format)
expression 代表 Application 物件的變數。
參數
| 名稱 | 必要/選用 | 資料類型 | 描述 |
|---|---|---|---|
| StringOrNumber | 必要 | Variant | 要格式化的字串或數字;可以傳遞為字串、浮點數或整數。 |
| UnitsIn | 必要 | Variant | 歸類至 StringOrNumber 所用的度量單位。 |
| UnitsOut | 必要 | Variant | 表示結果所用的度量單位。 |
| Format | 必要 | 字串 | 結果字串外觀的圖片。 |
傳回值
字串
註解
如果是以字串的形式傳遞,則 StringOrNumber 可能是儲存格的公式或預期的公式,或是以字串表示之儲存格的結果或預期結果。 FormatResult 方法會評估此字串,並格式化所產生的結果。 由於此字串是在特定儲存格的公式內容之外進行評估,所以如果此字串有包含任何儲存格參考,則 FormatResult 方法會傳回錯誤。
StringOrNumber 可能的值包括:
- 1.7
- 3
- "2.5"
- "4.1 cm"
- "12 ft - 17 in. + (12 cm / SQRT(7))"
UnitsIn 和 UnitsOut 引數可以是字串,例如 “inches”、“inch”、“in.” 或 “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"or'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 支援與意見反應。