You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修正VBA货币数字转英文文本代码,实现符合语法规范的带逗号与连字符的金额拼写

修正VBA货币数字转英文文本代码,实现符合语法规范的带逗号与连字符的金额拼写

Absolutely! You can tweak your existing VBA code to add proper commas between number groups and hyphens within two-digit numbers (like 29 → Twenty-Nine) to match the grammatical English currency format you want. Here's the revised code with those fixes:

'Main Function
Function SpellNumber(ByVal MyNumber)
    Dim Dollars, Cents, Temp
    Dim DecimalPlace, Count
    ReDim Place(9) As String
    Place(2) = " Thousand, "
    Place(3) = " Million, "
    Place(4) = " Billion, "
    Place(5) = " Trillion, "

    MyNumber = Trim(Str(MyNumber))
    DecimalPlace = InStr(MyNumber, ".")
    
    ' Handle cents if present
    If DecimalPlace > 0 Then
        Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
        MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
    End If

    Count = 1
    Do While MyNumber <> ""
        Temp = GetHundreds(Right(MyNumber, 3))
        If Temp <> "" Then 
            Dollars = Temp & Place(Count) & Dollars
        End If
        If Len(MyNumber) > 3 Then
            MyNumber = Left(MyNumber, Len(MyNumber) - 3)
        Else
            MyNumber = ""
        End If
        Count = Count + 1
    Loop

    ' Clean up trailing commas and adjust wording
    Select Case Dollars
        Case ""
            Dollars = "Zero Dollars"
        Case "One"
            Dollars = "One Dollar"
        Case Else
            ' Remove any trailing comma and space before "Dollars"
            Dollars = Trim(Left(Dollars, Len(Dollars) - 2)) & " Dollars"
    End Select

    ' Add cents if applicable
    If Cents <> "" Then
        Select Case Cents
            Case "One"
                Cents = " and One Cent"
            Case Else
                Cents = " and " & Cents & " Cents"
        End Select
        Dollars = Dollars & Cents
    End If

    SpellNumber = Dollars
End Function

' Converts a number from 100-999 into text
Function GetHundreds(ByVal MyNumber)
    Dim Result As String
    If Val(MyNumber) = 0 Then Exit Function
    MyNumber = Right("000" & MyNumber, 3)
    
    ' Convert the hundreds place
    If Mid(MyNumber, 1, 1) <> "0" Then
        Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
    End If
    
    ' Convert the tens and ones place
    If Mid(MyNumber, 2, 1) <> "0" Then
        Result = Result & GetTens(Mid(MyNumber, 2, 2))
    Else
        Result = Result & GetDigit(Mid(MyNumber, 3, 1))
    End If

    GetHundreds = Trim(Result)
End Function

' Converts a number from 10-99 into text
Function GetTens(TensText)
    Dim Result As String
    Result = "" ' Null out the temporary function value
    If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19
        Select Case Val(TensText)
            Case 10: Result = "Ten"
            Case 11: Result = "Eleven"
            Case 12: Result = "Twelve"
            Case 13: Result = "Thirteen"
            Case 14: Result = "Fourteen"
            Case 15: Result = "Fifteen"
            Case 16: Result = "Sixteen"
            Case 17: Result = "Seventeen"
            Case 18: Result = "Eighteen"
            Case 19: Result = "Nineteen"
        End Select
    Else ' If value between 20-99
        Select Case Val(Left(TensText, 1))
            Case 2: Result = "Twenty"
            Case 3: Result = "Thirty"
            Case 4: Result = "Forty"
            Case 5: Result = "Fifty"
            Case 6: Result = "Sixty"
            Case 7: Result = "Seventy"
            Case 8: Result = "Eighty"
            Case 9: Result = "Ninety"
        End Select
        ' Add hyphen for numbers 21-99 where ones digit is not zero
        If Val(Right(TensText, 1)) <> 0 Then
            Result = Result & "-" & GetDigit(Right(TensText, 1))
        End If
    End If
    GetTens = Trim(Result)
End Function

' Converts a number from 1-9 into text
Function GetDigit(Digit)
    Select Case Val(Digit)
        Case 1: GetDigit = "One"
        Case 2: GetDigit = "Two"
        Case 3: GetDigit = "Three"
        Case 4: GetDigit = "Four"
        Case 5: GetDigit = "Five"
        Case 6: GetDigit = "Six"
        Case 7: GetDigit = "Seven"
        Case 8: GetDigit = "Eight"
        Case 9: GetDigit = "Nine"
        Case Else: GetDigit = ""
    End Select
End Function

Key Fixes Explained:

  • Hyphens for two-digit numbers: The GetTens function now adds a hyphen between the tens and ones place for numbers 21-99 (e.g., 29 becomes "Twenty-Nine") instead of a space.
  • Commas between number groups: We updated the Place array to include commas (like " Thousand, "), and added logic to trim any trailing comma before appending "Dollars" for a clean finish.
  • Polished spacing: Removed extra spaces around hyphens and commas to ensure the output reads naturally like standard English currency formatting.

Example Outputs:

  • Input: 113729 → Output: One Hundred Thirteen Thousand, Seven Hundred Twenty-Nine Dollars
  • Input: 1113729 → Output: One Million, One Hundred Thirteen Thousand, Seven Hundred Twenty-Nine Dollars
  • Input: 52.75 → Output: Fifty-Two Dollars and Seventy-Five Cents

备注:内容来源于stack exchange,提问作者Jessica

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 11:28:21