Documente Academic
Documente Profesional
Documente Cultură
words
1. In MS Excel Press ALT+F11 to start the Visual Basic Editor.
2. On the Insert menu, click Module.
3. Copy & Paste the following code into the module sheet.
------------ --------- --------- --------- --------- ---Option Explicit
'Main Function
Function HST(ByVal MyNum)
Dim Rs, Ps, Temp
Dim DecPlac, Count
ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "
' String representation of amount.
MyNum = Trim(Str(MyNum) )
' Position of decimal place 0 if none.
DecPlac = InStr(MyNum, ".")
' Convert Paise and set MyNum to Rupee amount.
If DecPlac > 0 Then
Ps = GetTens(Left( Mid(MyNum, DecPlac + 1) & _
"00", 2))
MyNum = Trim(Left(MyNum, DecPlac - 1))
End If
Count = 1
Do While MyNum <> ""
Temp = GetHundreds( Right(MyNum, 3))
If Temp <> "" Then Rs = Temp & Place(Count) & Rs
If Len(MyNum) > 3 Then
MyNum = Left(MyNum, Len(MyNum) - 3)
Else
MyNum = ""
End If
Count = Count + 1
Loop
Select Case Rs
Case ""
Rs = "No Rupees"
Case "One"
Rs = "One Rupee"
Case Else
Rs = Rs & " Rupees"
End Select
Select Case Ps
Case ""
Ps = " and No Paise"
Case "One"
Ps = " and One Paisa"
Case Else
Ps = " and " & Ps & " Paise"
End Select
HST = Rs & Ps
End Function
' Converts a number from 100-999 into text
Function GetHundreds( ByVal MyNum)
Dim Result As String
If Val(MyNum) = 0 Then Exit Function
MyNum = Right("000" & MyNum, 3)
' Convert the hundreds place.
If Mid(MyNum, 1, 1) <> "0" Then
Result = GetDigit(Mid( MyNum, 1, 1)) & " Hundred "
End If
' Convert the tens and ones place.
If Mid(MyNum, 2, 1) <> "0" Then
Result = Result & GetTens(Mid( MyNum, 2))
Else
Result = Result & GetDigit(Mid( MyNum, 3))
End If
GetHundreds = Result
End Function
' Converts a number from 10 to 99 into text.
Function GetTens(TensText)
Dim Result As String
Result = "" ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
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"
------------ --------- --------- --------- --How to use the HST sample function
1. Direct Entry method
=HST(32.50)
2. Cell reference method
=HST(A1)