使用VBA将数字转换为单词

Ama*_*ath 3 excel vba numbers excel-vba text-manipulation

我有一列数字.在下一列中,我想要数字的文本/单词转换.

示例: 123.561将转换为One hundred twenty three point five six one.

我不想转换为货币,只是数字到文本,具有任意数量的小数位.

示例图片

我怎样才能做到这一点?

ash*_*awg 10

编辑:我已将以下程序改为非货币,无限小数位.

编辑2通过(1)(2)中的两个更改来考虑国际化,以使代码与其他小数分隔符(例如欧洲中部的冒号)一起工作' - 请参阅注释 Function SpellNumber Function fractionWords


例:

MsgBox SpellNumber(2123.4575)
Run Code Online (Sandbox Code Playgroud)

...的回报:

Two Thousand One Hundred Twenty Three point Four Five Seven Five


将以下代码粘贴到新模块中:

Option Explicit

Function SpellNumber(ByVal numIn)
    Dim LSide, RSide, Temp, DecPlace, Count, oNum
    oNum = numIn
    ReDim Place(9) As String
    Place(2) = " Thousand "
    Place(3) = " Million "
    Place(4) = " Billion "
    Place(5) = " Trillion "
    numIn = Trim(Str(numIn)) 'String representation of amount
    ' Edit 2.(0)/Internationalisation
    ' Don't change point sign here as the above assignment preserves the point!
    DecPlace = InStr(numIn, ".") 'Pos of dec place 0 if none 
    If DecPlace > 0 Then 'Convert Right & set numIn
        RSide = GetTens(Left(Mid(numIn, DecPlace + 1) & "00", 2))
        numIn = Trim(Left(numIn, DecPlace - 1))
    End If
    RSide = numIn
    Count = 1
    Do While numIn <> ""
        Temp = GetHundreds(Right(numIn, 3))
        If Temp <> "" Then LSide = Temp & Place(Count) & LSide
        If Len(numIn) > 3 Then
            numIn = Left(numIn, Len(numIn) - 3)
        Else
            numIn = ""
        End If
        Count = Count + 1
    Loop

    SpellNumber = LSide
    If InStr(oNum, Application.DecimalSeparator) > 0 Then    ' << Edit 2.(1) 
        SpellNumber = SpellNumber & " point " & fractionWords(oNum)
    End If

End Function

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

Function GetTens(TensText)  'Converts a number from 10 to 99 into text
    Dim w As String
    w = ""           'Null out the temporary function value
    If Val(Left(TensText, 1)) = 1 Then   'If value between 10-19
        Select Case Val(TensText)
            Case 10: w = "Ten"
            Case 11: w = "Eleven"
            Case 12: w = "Twelve"
            Case 13: w = "Thirteen"
            Case 14: w = "Fourteen"
            Case 15: w = "Fifteen"
            Case 16: w = "Sixteen"
            Case 17: w = "Seventeen"
            Case 18: w = "Eighteen"
            Case 19: w = "Nineteen"
            Case Else
        End Select
    Else      'If value between 20-99..
        Select Case Val(Left(TensText, 1))
            Case 2: w = "Twenty "
            Case 3: w = "Thirty "
            Case 4: w = "Forty "
            Case 5: w = "Fifty "
            Case 6: w = "Sixty "
            Case 7: w = "Seventy "
            Case 8: w = "Eighty "
            Case 9: w = "Ninety "
            Case Else
        End Select
        w = w & GetDigit _
            (Right(TensText, 1))  'Retrieve ones place
    End If
    GetTens = w
End Function

Function GetDigit(Digit) 'Converts a number from 1 to 9 into text
    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

Function fractionWords(n) As String
    Dim fraction As String, x As Long
    fraction = Split(n, Application.DecimalSeparator)(1)   ' << Edit 2.(2)
    For x = 1 To Len(fraction)
        If fractionWords <> "" Then fractionWords = fractionWords & " "
        fractionWords = fractionWords & GetDigit(Mid(fraction, x, 1))
    Next x
End Function
Run Code Online (Sandbox Code Playgroud)

(改编自来源:微软)


在线还有其他几个例子.如果您正在搜索"将数字转换为文本",那么您可能已经找到了它们,因为这意味着要更改数​​据类型.一个更好的搜索词是" vba将数字转换为单词 ".

  • @ashleedawg明文代码的长度是无关紧要的.代码文本转换为p代码,然后解释,然后作为exe代码运行.重要的是执行期间使用的字节.`vbNullString`是0字节,并且每次遇到像``"`这样的字符串文字时都不需要创建,填充和销毁内存地址,因此它在运行时使用较少的内存,并且运行速度更快.Rubberduck VBA插件(我是其贡献者)将识别并修复这些性能改进机会. (3认同)
  • @ashleedawg`Left`函数实际上是隐藏函数`_B_var_Left`的别名,用于接受和返回`Variant`,而`Left $`函数实际上是`_B_str_Left`函数的别名,是旨在接受并返回一个`String`.如果您的代码传入和/或期望返回一个`String`,那么您的代码将更有效地运行,因为您不会遇到从`String`到`Variant`的隐式转换,然后`Variant`返回到`String` on*every*调用函数,如`Left`,`Right`,`Mid`,`Trim`等. (3认同)
  • 通过使用`String`返回函数`Left $`,`Right $`,`Mid $`和`Trim $`,你将获得更好的性能.应该避免使用`Val`函数,因为它不能识别区域设置并且会在某些区域返回错误值 - 最好在适当时使用`CDbl`或`CLng`.另外,使`SpellNumber`的返回类型成为显式`String`,并强烈键入所有变量和辅助函数.最后,使用`vbNullString`比使用``"`更好. (2认同)
  • @ashleedawg好吧,存储很便宜,内存很昂贵。同样,在大多数情况下,您的存储空间要多于可用内存。因此,我总是喜欢在内存和速度上进行优化而不是在存储上进行优化。代码长度永远不会成为问题(如果它的长度优化了内存或速度)。我从未听说过有人因为VBA代码而用完存储空间,但是我听到了很多存储空间用完的消息!*(但这现在变得不合时宜了)* (2认同)
  • @ashleedawg [Rubberduck的家](http://www.rubberduckvba.com/).关于````vs`vbNullString`辩论:要考虑的另一个问题是程序员的*意图*(/代码清晰度) - `vbNullString`在传达意图方面要好得多:是的,我真的想要零 - 长度字符串在这里 - 我不小心忘记在引号之间放置有意义的字符.它基本上消除了一个误解/错误的来源. (2认同)