以下是一个简单的VBA代码示例,可用于将Word文档中所选单元格中的数字金额转换为大写金额:
``` Sub ConvertCurrencyToWords() Dim cell As Range For Each cell In Selection.Cells If IsNumeric(cell.Value) Then cell.Value = Format(cell.Value, "#,##0.00") cell.Value = "RMB " & ConvertToWords(cell.Value) & " only" End If Next End Sub
Function ConvertToWords(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 " MyNumber = Trim(Str(MyNumber)) DecimalPlace = InStr(MyNumber, ".") If DecimalPlace > 0 Then Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2)) MyNumber = Trim(Left(MyNumber, DecimalPlace - 1)) End If Count = 1