Excel 如何把数字转换为印度卢比的文字
当我们需要在Excel中把数字转换成印度卢比的文字时,如果我们手动操作,可能是一个很耗时的过程,因为我们需要输入大量的数字。通过使用VBA程序,我们可以更快完成这个耗时的过程。尽管VBA代码会很冗长,但如果有很多数值需要转换,我们可以使用这个过程。阅读本教程,了解如何在Excel中把数字转换成印度卢比的文字。
在Excel中把数字转换为印度卢比的文字
在这里,我们将创建一个新的VBA模块,然后使用公式来获得任何一个结果,并使用自动填充手柄来获得所有的结果。让我们看看一个简单的过程,了解如何在Excel中把数字转换成印度卢比的文字。
第1步
考虑一个有数字列表的Excel表格,与下面的图片类似。
现在右击工作表名称,选择查看代码,打开VBA应用程序,点击插入并选择模块。
**右键单击 > 查看代码 > 插入 > 模块 **
第2步
然后,如下图所示,在文本框中输入以下程序。
程序
Public Function RupeeFormat(SNum As String)
'Update By Nirmal
Dim xDPInt As Integer
Dim xArrPlace As Variant
Dim xRStr_Paisas As String
Dim xNumStr As String
Dim xF As Integer
Dim xTemp As String
Dim xStrTemp As String
Dim xRStr As String
Dim xLp As Integer
xArrPlace = Array("", "", " Thousand ", " Lacs ", " Crores ", " Trillion ", "", "", "", "")
On Error Resume Next
If SNum = "" Then
RupeeFormat = ""
Exit Function
End If
xNumStr = Trim(Str(SNum))
If xNumStr = "" Then
RupeeFormat = ""
Exit Function
End If
xRStr = ""
xLp = 0
If (xNumStr > 999999999.99) Then
RupeeFormat = "Digit excced Maximum limit"
Exit Function
End If
xDPInt = InStr(xNumStr, ".")
If xDPInt > 0 Then
If (Len(xNumStr) - xDPInt) = 1 Then
xRStr_Paisas = RupeeFormat_GetT(Left(Mid(xNumStr, xDPInt + 1) & "0", 2))
ElseIf (Len(xNumStr) - xDPInt) > 1 Then
xRStr_Paisas = RupeeFormat_GetT(Left(Mid(xNumStr, xDPInt + 1), 2))
End If
xNumStr = Trim(Left(xNumStr, xDPInt - 1))
End If
xF = 1
Do While xNumStr <> ""
If (xF >= 2) Then
xTemp = Right(xNumStr, 2)
Else
If (Len(xNumStr) = 2) Then
xTemp = Right(xNumStr, 2)
ElseIf (Len(xNumStr) = 1) Then
xTemp = Right(xNumStr, 1)
Else
xTemp = Right(xNumStr, 3)
End If
End If
xStrTemp = ""
If Val(xTemp) > 99 Then
xStrTemp = RupeeFormat_GetH(Right(xTemp, 3), xLp)
If Right(Trim(xStrTemp), 3) <> "Lac" Then
xLp = xLp + 1
End If
ElseIf Val(xTemp) <= 99 And Val(xTemp) > 9 Then
xStrTemp = RupeeFormat_GetT(Right(xTemp, 2))
ElseIf Val(xTemp) < 10 Then
xStrTemp = RupeeFormat_GetD(Right(xTemp, 2))
End If
If xStrTemp <> "" Then
xRStr = xStrTemp & xArrPlace(xF) & xRStr
End If
If xF = 2 Then
If Len(xNumStr) = 1 Then
xNumStr = ""
Else
xNumStr = Left(xNumStr, Len(xNumStr) - 2)
End If
ElseIf xF = 3 Then
If Len(xNumStr) >= 3 Then
xNumStr = Left(xNumStr, Len(xNumStr) - 2)
Else
xNumStr = ""
End If
ElseIf xF = 4 Then
xNumStr = ""
Else
If Len(xNumStr) <= 2 Then
xNumStr = ""
Else
xNumStr = Left(xNumStr, Len(xNumStr) - 3)
End If
End If
xF = xF + 1
Loop
If xRStr = "" Then
xRStr = "No Rupees"
Else
xRStr = " Rupees " & xRStr
End If
If xRStr_Paisas <> "" Then
xRStr_Paisas = " and " & xRStr_Paisas & " Paisas"
End If
RupeeFormat = xRStr & xRStr_Paisas & " Only"
End Function
Function RupeeFormat_GetH(xStrH As String, xLp As Integer)
Dim xRStr As String
If Val(xStrH) < 1 Then
RupeeFormat_GetH = ""
Exit Function
Else
xStrH = Right("000" & xStrH, 3)
If Mid(xStrH, 1, 1) <> "0" Then
If (xLp > 0) Then
xRStr = RupeeFormat_GetD(Mid(xStrH, 1, 1)) & " Lac "
Else
xRStr = RupeeFormat_GetD(Mid(xStrH, 1, 1)) & " Hundred "
End If
End If
If Mid(xStrH, 2, 1) <> "0" Then
xRStr = xRStr & RupeeFormat_GetT(Mid(xStrH, 2))
Else
xRStr = xRStr & RupeeFormat_GetD(Mid(xStrH, 3))
End If
End If
RupeeFormat_GetH = xRStr
End Function
Function RupeeFormat_GetT(xTStr As String)
Dim xTArr1 As Variant
Dim xTArr2 As Variant
Dim xRStr As String
xTArr1 = Array("Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
xTArr2 = Array("", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")
Result = ""
If Val(Left(xTStr, 1)) = 1 Then
xRStr = xTArr1(Val(Mid(xTStr, 2, 1)))
Else
If Val(Left(xTStr, 1)) > 0 Then
xRStr = xTArr2(Val(Left(xTStr, 1)) - 1)
End If
xRStr = xRStr & RupeeFormat_GetD(Right(xTStr, 1))
End If
RupeeFormat_GetT = xRStr
End Function
Function RupeeFormat_GetD(xDStr As String)
Dim xArr_1() As Variant
xArr_1 = Array(" One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine", "")
If Val(xDStr) > 0 Then
RupeeFormat_GetD = xArr_1(Val(xDStr) - 1)
Else
RupeeFormat_GetD = ""
End If
End Function
第3步
然后,使用ALT+Q命令,将工作表保存为支持宏的工作簿,并退出vba应用程序。
然后,在Excel工作表中,点击一个空单元格,输入=RupeeFormat(A2),并按回车键,得到我们的第一个结果。
**保存 > ALT + Q > 空单元格 > 公式 > 回车 **
第4步
为了得到所有的结果,使用自动填充手柄从第一个结果开始向下拖动,我们的最终结果将类似于下面的图片。
结论
在本教程中,我们用一个简单的例子来演示如何在Excel中把数字转换成文字。