在word里面把数字变成大写的金额,帮我写一段vba代码实现此功能

动态 未结 0 63
小小草
小小草 LV1 2023年9月23日 23:40 发表
以下是将数字转换为大写金额的VBA代码: Sub ConvertToWords() Dim MyNumber As Currency Dim Dollars As String Dim Cents As String Dim Temp As String Dim DecimalPlace As Integer Dim Count As Integer ' Change this variable to the currency you want to use Const CurrencyName = "Dollars" ' Set up test values MyNumber = Range("A1").Value ' Check for zero value If MyNumber = 0 Then Range("B1").Value = "Zero " & CurrencyName Exit Sub End If ' Check for negative value If MyNumber < 0 Then MyNumber = Abs(MyNumber) Temp = "Negative " End If ' Format number as currency Dollars = Format(Int(MyNumber), "#,##0") Cents = Format((MyNumber - Int(MyNumber)) * 100, "00") ' Find decimal place DecimalPlace = InStr(Dollars, ".") ' Convert cents and set up cents string If DecimalPlace > 0 Then Temp = GetTens(Left(Cents, 2)) Cents = Right(Cents, Len(Cents) - 2) End If Cents = Temp & GetTens(Cents) & " Cents" ' Convert dollars and set up dollars string Temp = "" Count = 1 Do While Dollars <> "" Temp = GetHundreds(Right(Dollars, 3)) & Temp If Len(Dollars) > 3 Then Dollars = Left(Dollars, Len(Dollars) - 3) Else Dollars = "" End If If Dollars <> "" Then Temp = " " & GetPlace(Count) & " " & Temp End If Count = Count + 1 Loop ' Set up final string Range("B1").Value = Trim(Temp) & " " & CurrencyName End Sub Function GetHundreds(ByVal MyNumber) Dim Result As String If Val(MyNumber) = 0 Then Exit Function MyNumber = Right("000" & MyNumber, 3) ' Convert the hundreds place.
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复