vba - 使用 VBA 将数字转换为单词

标签 vba excel numbers text-manipulation

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

示例:123.561 将转换为一百二十三点五六一

我不想转换为货币,只是将数字转换为文本,小数位数不限。

example image

我该怎么做?

最佳答案

编辑:我已将以下过程改编为非货币、无限小数位。

编辑 2 通过 (1) Function SpellNumber(2 中的两项更改考虑国际化 ) 函数fractionWords,使代码与其他小数分隔符一起使用(例如中欧的冒号) ' - see comment

<小时/>

示例:

MsgBox SpellNumber(2123.4575)

...返回:

两千一百二十三点四五七五

<小时/>

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

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

(改编自 Source: Microsoft )

<小时/>

网上还有其他几个例子。如果您搜索“将数字转换为文本”,您可能已经找到了它们,因为这意味着更改数据类型。更好的搜索词是"vba convert numbers to words "

关于vba - 使用 VBA 将数字转换为单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51204004/

相关文章:

vba - 具有多个条件都等于相同值的 IF 语句的替代方案

ms-access - Access Report 不允许我引用 VBA 中的字段,除非它本身就在报告中

excel - 使用 UTF-8 编码的 VBA Excel 宏写入文件

excel - 从 primefaces 数据表导出的 excel 文件中不显示标题

JavaScript Number 保留前导 0

python - 为什么 9007199254740993 != 9007199254740993.0?

excel - 去除命名范围左侧的空列

excel - 计算包含特定值的单元格

excel - SUM 数字,但当为 0 时,加 8 代替

android - 如何在 Kotlin 中删除小数末尾多余的零?