2024年4月27日发(作者:)
excel中人民币金额大写的函数
在 Excel 中,如果你想将人民币金额转换为大写形式,你可以使用以下步骤:
1. 下载并安装 VBA 代码模块:
- 打开 Excel 文件。
- 按下 `Alt` + `F11` 打开 Visual Basic for Applications (VBA) 编辑器。
- 在菜单中选择 "插入" -> "模块",这将创建一个新的代码模块。
2. 复制并粘贴 VBA 代码:
- 将以下 VBA 代码复制并粘贴到新建的代码模块中:
```vba
Function RMBFormat(ByVal MyNumber)
Dim Units As String
Dim DecimalPlace As String
Dim TempStr As String
Dim DecimalSeparator As String
Dim UnitName As String
Dim Count As Integer
Dim DecimalSeparatorLength As Integer
ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "
' String representation of amount.
MyNumber = Trim(CStr(MyNumber))
' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
TempStr = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
TempStr = GetHundreds(Right(MyNumber, 3))
If TempStr <> "" Then Units = Place(Count) & TempStr & Units
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
RMBFormat = Units & GetTens(TempStr)
End Function
Private Function GetTens(TensText)
Dim Result As String
Result = "" ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ' If value
Select Case Val(TensText)
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
Else ' If value
Select Case Val(Left(TensText, 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
Result = Result & GetDigit _
(Right(TensText, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function
Private Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = "One"
Case 2: GetDigit = "Two"
Case 3: GetDigit = "Three"
Case 4: GetDigit = "Four"
Case 5: GetDigit = "Five"
Case 6: GetDigit = "Six"
Case 7: GetDigit = "Seven"
Case 8: GetDigit = "Eight"
Case 9: GetDigit = "Nine"
Case Else: GetDigit = ""
End Select
End Function
Private Function GetHundreds(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.
Result = Result & GetTens(Mid(MyNumber, 2))
GetHundreds = Result
End Function
```
3. 使用自定义函数:
- 保存并关闭 VBA 编辑器。
- 在 Excel 工作表中输入你的人民币金额,例如在 A1 单元格中输入 `12345.67`。
- 在 B1 单元格中输入以下公式:
```excel
=RMBFormat(A1)
```
- 按下 `Enter` 键,B1 单元格将显示人民币金额的大写形式。
这个 VBA 代码模块定义了一个名为 `RMBFormat` 的函数,该函数接受一个数值参数并返
回其人民币金额的大写形式。确保在使用之前允许宏并保存 Excel 文件。这只是一个简单的
示例,你可能需要根据实际需求进行适当的修改。
发布评论