在VBA中将数字修改为字母,以便单词不以“一个”开头

时间:2018-10-30 07:49:55

标签: excel vba

我的问题是,我想修改将数字转换为单词的VBA代码,所以现在我不希望单词以“一个”开头,让您不清楚:< / p>

当前VBA结果:

100  One Hundred
1000 One Thousand

所需的VBA结果:

100  Hundred
1000 Thousand
4000 four thousand
5466 five thousand four hundred sixty six

原因:因为我想将其转换为土耳其语,然后用土耳其语说一百个“ BirYüz”是错误的,所以土耳其语中正确的语言是一百个“Yüz”。

因此,我不是VBA中的专家,我该如何处理?

下面是VBA代码:

Function NumberstoWords(ByVal pNumber)
    'Updateby20140220
    Dim Dollars
    arr = Array("", "", " Thousand ", " Million ", " Billion ", " Trillion ")
    pNumber = Trim(Str(pNumber))
    xDecimal = InStr(pNumber, ".")
    If xDecimal > 0 Then
        pNumber = Trim(Left(pNumber, xDecimal - 1))
    End If

    xIndex = 1
    Do While pNumber <> ""
        xHundred = ""
        xValue = Right(pNumber, 3)
        If Val(xValue) <> 0 Then
            xValue = Right("000" & xValue, 3)
            If Mid(xValue, 1, 1) <> "0" Then
                xHundred = GetDigit(Mid(xValue, 1, 1)) & " Hundred "
            End If
            If Mid(xValue, 2, 1) <> "0" Then
                xHundred = xHundred & GetTens(Mid(xValue, 2))
            Else
                xHundred = xHundred & GetDigit(Mid(xValue, 3))
            End If
        End If
        If xHundred <> "" Then
            Dollars = xHundred & arr(xIndex) & Dollars
        End If
        If Len(pNumber) > 3 Then
            pNumber = Left(pNumber, Len(pNumber) - 3)
        Else
            pNumber = ""
        End If
        xIndex = xIndex + 1
    Loop
    NumberstoWords = Dollars
End Function

Function GetTens(pTens)
    Dim Result As String
    Result = ""
    If Val(Left(pTens, 1)) = 1 Then
        Select Case Val(pTens)
        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"
        Case Else
        End Select
    Else
        Select Case Val(Left(pTens, 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 "
        Case Else
        End Select
        Result = Result & GetDigit(Right(pTens, 1))
    End If
    GetTens = Result
End Function

Function GetDigit(pDigit)
    Select Case Val(pDigit)
    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

2 个答案:

答案 0 :(得分:4)

在函数 NumberstoWords()的末尾添加此内容:

If LCase(Left(NumberstoWords, 3)) = "one" And (Len(NumberstoWords) > Len("One")) Then
    NumberstoWords = Trim(Right(NumberstoWords, Len(NumberstoWords) - Len("One")))
End If

它将检查单词的前三个字符“ one”,并确保它本身不是单词“ one”。这就是它产生的结果,请考虑添加另一条规则以避免150

?NumberstoWords(150)
Hundred Fifty
?NumberstoWords(1)
One
?NumberstoWords(100)
Hundred
?NumberstoWords(91)
Ninety One
?NumberstoWords(5466)
Five Thousand Four Hundred Sixty Six

答案 1 :(得分:1)

更改此行

Dollars = xHundred & arr(xIndex) & Dollars

Dollars = IIf(xHundred <> "One" Or arr(xIndex) = "", Replace(xHundred, "One ", "") & arr(xIndex), Trim(arr(xIndex)) & " ") & Dollars

为说明起见,为了清楚起见,我将下划线(_)替换为空格(“”):

  • 如果xHundred是“一个”,并且我们正在处理“ _Thousand_”或“ _Million _”(依此类推),则它将添加“ Thousand_”或“ Million_”。
  • 否则,它将用任何内容(“”)替换“ One_”,并将“ _Thousand_”或“ _Million_”添加到末尾。
  • 这会将“ One_Hundred_One”更改为“ Hundred_Seven”,但将“ Seven_Hundred_One”保留不变。