Coding Examples

Some Coding Examples

Num2Char_Code

'---------------------------------------------------------------------------------------   
' Module    : bas_Num2Char   
' DateTime  : 20/10/2013   
' Author    : CA Vikram Shankar Mathur   
' Purpose   :   
'---------------------------------------------------------------------------------------   
Option Explicit   
'   
' Num2Char Macro   
' Macro recorded 01/01/2000 by Vikram S. Mathur, ACA   
'   
'   Public Function Num2Char(Number As Double) As String   
'---------------------------------------------------------------------------------------   
' Procedure : Num2Char   
' DateTime  : 20/10/2013   
' Author    : CA Vikram Shankar Mathur   
' Purpose   :   
'---------------------------------------------------------------------------------------   
'   
    Dim nNumb As Double   
    Dim nCror As Double   
    Dim nLacs As Double   
    Dim nThou As Double   
    Dim nHund As Double   
    Dim nUnit As Double   
    Dim nDeci As Double   
    Dim nThouCror As Double   
    Dim nHunCror As Double   
    Dim lCrorIsHun As Boolean   
    Dim lCrorIsThou As Boolean   
    ' declarations are now over, now evaluate given number   
    ' First assign the passed number to a Double variable   
    On Error GoTo Num2Char_Error      
        nNumb = Number   
        If nNumb > 999999999.99 Then   
            Num2Char = "<< Cannot display output if greater than 99,99,99,999.99 >>"   
            Exit Function   
        End If   
        nCror = Int(nNumb / 10000000)   
        nNumb = nNumb - (nCror * 10000000)   
        If nCror > 1000 Then   
            lCrorIsThou = True   
            lCrorIsHun = False   
        ElseIf nCror > 100 Then   
            lCrorIsThou = False   
            lCrorIsHun = True   
        Else   
            lCrorIsThou = False   
            lCrorIsHun = False   
        End If   
        If lCrorIsThou Then   
            nThouCror = Int(nCror / 1000)   
            If nThouCror > 100 Then   
                Num2Char = Str(Int(nThouCror / 1000)) & "Thousand and"   nThouCror = nThouCror - (Int(nThouCror / 1000) * 1000)   
                nHunCror = Int((nCror - (nThouCror * 1000)) / 100)   
                Num2Char = Num2Char & aWord(nThouCror) & "Thousand" & aWord(nHunCror) & "Hundred and " & aWord(nCror) & " "   
            Else   
                nHunCror = Int((nCror - (nThouCror * 1000)) / 100)   
                nCror = nCror - (nThouCror * 1000) - (nHunCror * 100)   
                Num2Char = aWord(nThouCror) & "Thousand" & aWord(nHunCror) & "Hundred and " & aWord(nCror) & " "   
            End If   
        End If   
        If lCrorIsHun Then   
            nHunCror = Int(nCror / 100)   
            nCror = nCror - (nHunCror * 100)   
            Num2Char = aWord(nHunCror) & "Hundred and " & aWord(nCror) & " "   
        End If   
        If nCror > 0 Then   
            Num2Char = aWord(nCror) & " Crores "   
        Else   nCror = 0   
            Num2Char = ""   
        End If   
        If nNumb > 100000 Then   
            nLacs = Int(nNumb / 100000)   
            nNumb = nNumb - (nLacs * 100000)   
            Num2Char = Num2Char & aWord(nLacs) & " Lacs "   
        Else   
            nLacs = 0   
            Num2Char = Num2Char & ""   
        End If   
        If nNumb > 1000 Then   
            nThou = Int(nNumb / 1000)   
            nNumb = nNumb - (nThou * 1000)   
            Num2Char = Num2Char & aWord(nThou) & " Thousand "   
        Else   
            nThou = 0   
            Num2Char = Num2Char & ""   
        End If   
        If nNumb > 100 Then   
            nHund = Int(nNumb / 100)   
            nNumb = nNumb - (nHund * 100)   
            Num2Char = Num2Char & aWord(nHund) & " Hundred  "   
        Else   
            nHund = 0   
            Num2Char = Num2Char & ""   
        End If   
        If nNumb > 0 Then   
            nUnit = Int(nNumb)   
            nNumb = nNumb - nUnit   
            Num2Char = Num2Char & aWord(nUnit) & " and "   
        Else   
            nUnit = 0   
            Num2Char = Num2Char & " and "   
        End If   
        If (nNumb * 100) > 0 Then   
            nDeci = (nNumb * 100)   
            Num2Char = Num2Char & aWord(nDeci) & " PAISE ONLY"   
        Else   
            nDeci = 0   
            Num2Char = Num2Char & " NIL PAISE ONLY"   
        End If   
        Num2Char = StrConv(Num2Char, vbUpperCase)      
        On Error GoTo 0      
Num2Char_Exit:      
        Exit Function      
Num2Char_Error:      
        MsgBox "Error " & Err.Number & " on Line Number " & Erl & " (" & Err.Description & ") in procedure Num2Char of Module bas_Num2Char"   
        Resume Num2Char_Exit      
End Function   
'   
' aWord Macro   
' Macro recorded 01/01/2000 by Vikram S. Mathur, ACA   
'   
'   
Private Function aWord(i)   
'---------------------------------------------------------------------------------------   
' Procedure : aWord   
' DateTime  : 20/10/2013   
' Author    : CA Vikram Shankar Mathur   
' Purpose   :   
'---------------------------------------------------------------------------------------   
'   
    Dim cword(100) As String      
    On Error GoTo aWord_Error      
    cword(0) = "Nil"   
    cword(1) = "One"   
    cword(2) = "Two"   
    cword(3) = "Three"   
    cword(4) = "Four"   
    cword(5) = "Five"   
    cword(6) = "Six"   
    cword(7) = "Seven"   
    cword(8) = "Eight"   
    cword(9) = "Nine"   
    cword(10) = "Ten"   
    cword(11) = "Eleven"   
    cword(12) = "Twelve"   
    cword(13) = "Thirteen"   
    cword(14) = "Fourteen"   
    cword(15) = "Fifteen"   
    cword(16) = "Sixteen"   
    cword(17) = "Seventeen"   
    cword(18) = "Eighteen"   
    cword(19) = "Nineteen"   
    cword(20) = "Twenty"   
    cword(21) = "Twenty-one"   
    cword(22) = "Twenty-two"   
    cword(23) = "Twenty-three"   
    cword(24) = "Twenty-four"   
    cword(25) = "Twenty-five"   
    cword(26) = "Twenty-six"   
    cword(27) = "Twenty-seven"   
    cword(28) = "Twenty-eight"   
    cword(29) = "Twenty-nine"   
    cword(30) = "Thirty"   
    cword(31) = "Thirty-one"   
    cword(32) = "Thirty-two"   
    cword(33) = "Thirty-three"   
    cword(34) = "Thirty-four"   
    cword(35) = "Thirty-five"   
    cword(36) = "Thirty-six"   
    cword(37) = "Thirty-seven"   
    cword(38) = "Thirty-eight"   
    cword(39) = "Thirty-nine"   
    cword(40) = "Forty"   
    cword(41) = "Forty-one"   
    cword(42) = "Forty-two"   
    cword(43) = "Forty-three"   
    cword(44) = "Forty-four"   
    cword(45) = "Forty-five"   
    cword(46) = "Forty-six"   
    cword(47) = "Forty-seven"   
    cword(48) = "Forty-eight"   
    cword(49) = "Forty-nine"   
    cword(50) = "Fifty"   
    cword(51) = "Fifty-one"   
    cword(52) = "Fifty-two"   
    cword(53) = "Fifty-three"   
    cword(54) = "Fifty-four"   
    cword(55) = "Fifty-five"   
    cword(56) = "Fifty-six"   
    cword(57) = "Fifty-seven"   
    cword(58) = "Fifty-eight"   
    cword(59) = "Fifty-nine"   
    cword(60) = "Sixty"   
    cword(61) = "Sixty-one"   
    cword(62) = "Sixty-two"   
    cword(63) = "Sixty-three"   
    cword(64) = "Sixty-four"   
    cword(65) = "Sixty-five"   
    cword(66) = "Sixty-six"   
    cword(67) = "Sixty-seven"   
    cword(68) = "Sixty-eight"   
    cword(69) = "Sixty-nine"  
    cword(70) = "Seventy"   
    cword(71) = "Seventy-one"   
    cword(72) = "Seventy-two"   
    cword(73) = "Seventy-three"   
    cword(74) = "Seventy-four"   
    cword(75) = "Seventy-five"   
    cword(76) = "Seventy-six"   
    cword(77) = "Seventy-seven"   
    cword(78) = "Seventy-eight"   
    cword(79) = "Seventy-nine"   
    cword(80) = "Eighty"   
    cword(81) = "Eighty-one"   
    cword(82) = "Eighty-two"   
    cword(83) = "Eighty-three"   
    cword(84) = "Eighty-four"   
    cword(85) = "Eighty-five"   
    cword(86) = "Eighty-six"   
    cword(87) = "Eighty-seven"   
    cword(88) = "Eighty-eight"   
    cword(89) = "Eighty-nine"   
    cword(90) = "Ninety"   
    cword(91) = "Ninety-one"   
    cword(92) = "Ninety-two"   
    cword(93) = "Ninety-three"   
    cword(94) = "Ninety-four"   
    cword(95) = "Ninety-five"   
    cword(96) = "Ninety-six"   
    cword(97) = "Ninety-seven"   
    cword(98) = "Ninety-eight"   
    cword(99) = "Ninety-nine"   
    Select Case i   Case i   
        aWord = cword(i)   
    End Select      
    On Error GoTo 0      
aWord_Exit:      
        Exit Function      
aWord_Error:      
        MsgBox "Error " & Err.Number & " on Line Number " & Erl & " (" & 
        Err.Description & ") in procedure aWord of Module bas_Num2Char"   
        Resume  aWord_Exit         
End Function   
'---------------------------------------------------------------------------------------    

<<=== Back