将数字转换为Word-在两者之间添加“与”

时间:2018-07-05 04:49:26

标签: excel excel-vba excel-formula vba

我目前正在使用下面的vba将数字转换为单词。所有工作正常,例如:

520,000.00它将转换为五十万,但我想改为五百两万。

在哪里可以在以下公式中添加“和”

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 "
' String representation of amount.
MyNumber = Trim(Str(MyNumber))
' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert cents and set MyNumber to dollar amount.
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
    If Len(MyNumber) > 3 Then
        MyNumber = Left(MyNumber, Len(MyNumber) - 3)
    Else
        MyNumber = ""
    End If
    Count = Count + 1
Loop

Select Case Cents
    Case ""
        Cents = ""
    Case "One"
        Cents = " and One Cent"
          Case Else
        Cents = " and " & Cents & " Cents"
End Select
SpellNumber = Dollars & Cents
End Function

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))
Else
    Result = Result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = Result
End Function

1 个答案:

答案 0 :(得分:1)

逻辑是,如果您有一个大于零的百位数字,并且十位或单位是一个非零数字,则需要在工作“一百”之后添加“和”。

在代码后输入您的GetHundreds:

' Convert the hundreds place.
If Mid(MyNumber, 1, 1) <> "0" Then
    Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If

插入行

If CInt(MyNumber) > 100 Then Result = Result & "and "

看看是否适合您。

编辑:对我来说太聪明了。全部数百个不需要“和”,因此请尝试以下操作:

If CInt(MyNumber) Mod 100 <> 0 Then Result = Result & "and "