我有一個代碼,我有一個輸入框,我通常在其中復制一些數字。我需要 vba 來處理這些并將它們作為歐洲格式傳遞給單元格。傳入的格式是美國格式,如下所示:
##,### ## ##
我使用以下代碼將其轉換為我想要的格式:
dim earned as variant
earned = Replace(Me.txtEarnedG.Value, ".", "")
earned = Replace(earned, ",", "")
earned = Replace(earned, " ", "")
earned = Left(earned, Len(earned) - 4) & "," & Right(earned, 4)
amount_earned = CDbl(earned)
這非常有效,可以將
35,545 45 55(美國格式,帶空格,不帶“。”)轉換為 35545,4555(歐盟格式)
當我嘗試在其中一個空格之間轉換一個不是 2 位數字的數字時,我的問題就出現了,
35,545 4 13 becomes 3554,5413 instead of 35545,0413
35,545 12 4 becomes 3554,5124 instead of 35545,1204
我正在考慮以某種方式使用 Instr 和 right 。但我不知道如何輕松引入它。(數字應始終保留 4 位小數,但其余可能會有所不同,可能大于或小于千位)
uj5u.com熱心網友回復:
我認為您可以使用Split@Porcupine 建議的功能并結合使用Format以獲得您想要的數字格式。
下面是一個使用您的數字的簡單示例
Public Sub test()
Const TEST_NUM1 = "35,545 4 13"
Const TEST_NUM2 = "35,545 12 4"
Const TEST_NUM3 = "35,545 45 55"
FormatAsEU TEST_NUM1
FormatAsEU TEST_NUM2
FormatAsEU TEST_NUM3
End Sub
Public Function FormatAsEU(strNumber As String) As String
Dim varInput As Variant
varInput = Split(strNumber, " ")
FormatAsEU = Format(varInput(0), "#.#") & Format(varInput(1), "00") & Format(varInput(2), "00")
Debug.Print FormatAsEU
End Function
除錯結果:
> 35545.0413
> 35545.1204
> 35545.4555
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/486702.html
