The Will Will Web

記載著 Will 在網路世界的學習心得與技術分享

如何用 VBA 將傳進來的半型數字字串轉成國字大寫

' 將傳進來的半型數字字串轉成國字大寫
' -------------------------------------------
Function 轉國字(s As String) As String
  Dim s1 As String
  Dim s2 As Long
  If s = "" Then 轉國字 = "未輸入金額": Exit Function
  While Left(s, 1) = "0": s = Right(s, Len(s) - 1): Wend
  tmp節名 = "元萬億兆京"
  節數 = (Len(s) - 1) \ 4 + 1
  位數 = 節數 * 4
  s = Right("0000" & s, 位數)
  For i = 節數 To 1 Step -1
    個位名 = Mid(tmp節名, i, 1)
    s1 = Mid(s, ((節數 - i) * 4) + 1, 4)
    zero = ""
    If Left(s1, 1) = "0" Then zero = "零"
    tmp = tmp & zero & 轉四位數(s1) & 個位名
  Next
  If Left(tmp, 1) = "零" Then tmp = Right(tmp, Len(tmp) - 1)
  tmp = Replace(tmp, "零零", "零")
  tmp = Replace(tmp, "零萬", "")
  tmp = Replace(tmp, "零億", "")
  tmp = Replace(tmp, "零元", "元")
  轉國字 = tmp & "整"
End Function

... 繼續閱讀 ...