以下是将数字转换为大写金额的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.