VBA 金额转英文大写 数字转英文 EXCEL

由于自己英语差,懒得转写,就在网上找了个金额转英文大写的VBA程序,但是发现and有时候会漏加,自己就稍微修改了下,程序分享出来。

使用方法:EXCEL→开发工具→Visual Basic→插入模块→然后输入最下面的代码就好了。。当然也可以做成模块导入,方法自己网上查。

函数说明:ConvertCurrencyToEnglish(要转换的数字,[前缀],[后缀])  第一个参数必填,后两个选填。

不填写的话默认前缀是“SAY US DOLLAR”,后缀是“ ONLY***”。

代码:

Public Function ConvertCurrencyToEnglish(ByVal MyNumber, Optional ByVal leftstr = "SAY US DOLLAR ", Optional ByVal rightstr = " ONLY***")

         Dim Temp

         Dim Dollars, Cents

         Dim DecimalPlace, Count

         ReDim Place(9) As String

         Place(2) = " THOUSAND "

         Place(3) = " MILLION "

         Place(4) = " BILLION "

         Place(5) = " TRILLION "

         ' Convert MyNumber to a string, trimming extra spaces.

         MyNumber = Trim(Str(MyNumber))

         ' Find decimal place.

         DecimalPlace = InStr(MyNumber, ".")

         ' If we find decimal place...

         If DecimalPlace > 0 Then

            ' Convert cents

            Temp = Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2)

            Cents = ConvertTens(Temp)

            ' Strip off cents from remainder to convert.

            MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))

         End If

         Count = 1

         Do While MyNumber <> ""

            ' Convert last 3 digits of MyNumber to English dollars.

            Temp = ConvertHundreds(Right(MyNumber, 3))
            
            If Count = 1 And Mid(Right(MyNumber, 3), 1, 1) = 0 And Mid(Right(MyNumber, 3), 1, 3) <> "000" Then Temp = "AND " & Temp

            If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars

            If Len(MyNumber) > 3 Then

               ' Remove last 3 converted digits from MyNumber.

               MyNumber = Left(MyNumber, Len(MyNumber) - 3)

            Else

               MyNumber = ""

            End If

            Count = Count + 1

         Loop

         ' Clean up dollars.

         Select Case Dollars

            Case ""

               Dollars = "NO DOLLARS"

            Case "One"

               Dollars = "ONE DOLLAR"

            Case Else

               Dollars = Dollars

         End Select

         ' Clean up cents.

         Select Case Cents

            Case ""

               Cents = ""

            Case "One"

               Cents = " AND ONE CENT"

            Case Else

               Cents = " AND " & Cents & " CENTS"

         End Select

         ConvertCurrencyToEnglish = leftstr & Dollars & Cents & rightstr

End Function

Private Function ConvertHundreds(ByVal MyNumber)

Dim Result As String

         ' Exit if there is nothing to convert.

         If Val(MyNumber) = 0 Then Exit Function

         ' Append leading zeros to number.

         MyNumber = Right("000" & MyNumber, 3)

         ' Do we have a hundreds place digit to convert?

         If Left(MyNumber, 1) <> "0" Then
         
                Result = ConvertDigit(Left(MyNumber, 1)) & " HUNDRED "
                If Mid(MyNumber, 2, 2) <> "00" Then
                
                    Result = Result & "AND "
                    
                End If

         End If

         ' Do we have a tens place digit to convert?

         If Mid(MyNumber, 2, 1) <> "0" Then

            Result = Result & ConvertTens(Mid(MyNumber, 2))

         Else

            ' If not, then convert the ones place digit.

            Result = Result & ConvertDigit(Mid(MyNumber, 3))

         End If

         ConvertHundreds = Trim(Result)

End Function

Private Function ConvertTens(ByVal MyTens)

Dim Result As String

         ' Is value between 10 and 19?

         If Val(Left(MyTens, 1)) = 1 Then

            Select Case Val(MyTens)

               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

         ElseIf Val(Right(MyTens, 1)) = 0 Then

            ' .. otherwise it's between 20 and 90 step by 10.

            Select Case Val(MyTens)

               Case 20: Result = "TWENTY "

               Case 30: Result = "THIRTY "

               Case 40: Result = "FORTY "

               Case 50: Result = "FIFTY "

               Case 60: Result = "SIXTY "

               Case 70: Result = "SEVENTY "

               Case 80: Result = "EIGHTY "

               Case 90: Result = "NINETY "

               Case Else

            End Select
            
        Else
            ' .. otherwise it's between 20 and 99.

            Select Case Val(Left(MyTens, 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


            ' Convert ones place digit.

            Result = Result & ConvertDigit(Right(MyTens, 1))

         End If

         ConvertTens = Result

End Function

Private Function ConvertDigit(ByVal MyDigit)

Select Case Val(MyDigit)

            Case 1: ConvertDigit = "ONE"

            Case 2: ConvertDigit = "TWO"

            Case 3: ConvertDigit = "THREE"

            Case 4: ConvertDigit = "FOUR"

            Case 5: ConvertDigit = "FIVE"

            Case 6: ConvertDigit = "SIX"

            Case 7: ConvertDigit = "SEVEN"

            Case 8: ConvertDigit = "EIGHT"

            Case 9: ConvertDigit = "NINE"

            Case Else: ConvertDigit = ""

         End Select

End Function


 

 

你可能感兴趣的:(VBA 金额转英文大写 数字转英文 EXCEL)