Tuesday, March 28, 2023

How To Convert Number To Text In Excel

Convert Numbers to Text (NPR)

This VBA Code will create a simple function named "SpellNumber" that will basically convert the number into Text. We can get a lot of codes for conversion in English Currencies but this will convert in Nepali Currency Unit (i.e. Crore, Lakh etc.)

1. Open an Excel Sheet. 

2. Now open the VBA Code window by pressing Alt + F11

3.  In the VBA Tab click Insert Menu and Insert "Module".

4. Double Click the inserted Module and inside it paste the VBA Code provided below.

5. Now go back to excel sheet and you can use the function "SpellNumber"

6. So type =SpellNumber(select cell with number) and enter.

Function SpellNumber(ByVal MyNumber)
    Dim Rupees, Paise, Temp
    Dim DecimalPlace, Count
    
    ReDim Place(9) As String
    Place(2) = " Thousand "
    Place(3) = " Lakh "
    Place(4) = " Crore "
    Place(5) = " Arba "
    
    MyNumber = Trim(Str(MyNumber))
    
    DecimalPlace = InStr(MyNumber, ".")
    If DecimalPlace > 0 Then
        Paise = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
        MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
    End If
    
    Count = 1
    Do While MyNumber <> ""
       If Count <= 1 Then
                If Len(MyNumber) = 1 Then
                Temp = GetDigit(MyNumber)
                ElseIf Len(MyNumber) = 2 Then
                Temp = GetTens(MyNumber)
                Else
                Temp = GetHundreds(Right(MyNumber, 3))
                End If
                
                If Temp <> "" Then Rupees = Temp & Place(Count) & Rupees
                        
                    If Len(MyNumber) > 3 Then
                        MyNumber = Left(MyNumber, Len(MyNumber) - 3)
                    Else
                        MyNumber = ""
                    End If
                Count = Count + 1
        ElseIf Count > 5 Then
            Rupees = "This function only converts up to Arba."
            Paise = " "
            Exit Do
        Else
                If Len(MyNumber) = 1 Then
                Temp = GetDigit(MyNumber)
                ElseIf Len(MyNumber) = 2 Then
                Temp = GetTens(MyNumber)
                Else
                Temp = GetTens(Right(MyNumber, 2))
                End If
                
                    If Temp <> "" Then Rupees = Temp & Place(Count) & Rupees
                        
                    If Len(MyNumber) > 2 Then
                        MyNumber = Left(MyNumber, Len(MyNumber) - 2)
                    Else
                        MyNumber = ""
                    End If
                    
                Count = Count + 1
        End If
    
    Loop
    Select Case Rupees
        Case "This function only converts up to Arba."
            Rupees = Rupees
        Case ""
            Rupees = "No Rupees"
        Case "One"
            Rupees = "One Rupees"
         Case Else
            Rupees = Rupees & " Rupees"
    End Select
    Select Case Paise
        Case " "
            Paise = " "
        Case ""
            Paise = " and No Paisa"
        Case "One"
            Paise = " and One Paisa"
              Case Else
            Paise = " and " & Paise & " Paisa"
    End Select
    SpellNumber = Rupees & Paise
End Function
Function GetHundreds(ByVal MyNumber)
    Dim Result As String
    If MyNumber = 0 Then Exit Function
    If Mid(MyNumber, 1, 1) <> "0" Then
        Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
    End If
    If Mid(MyNumber, 2, 1) <> "0" Then
        Result = Result & GetTens(Mid(MyNumber, 2))
        Else: Result = Result & GetDigit(Mid(MyNumber, 3))
    End If
        GetHundreds = Result
End Function

Function GetTens(TensText)
    Dim Result As String
    Result = ""
    If Left(TensText, 1) = 1 Then
        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"
        End Select
    Else
        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 "
        End Select
        Result = Result & GetDigit(Right(TensText, 1))
    End If
    GetTens = Result
        
End Function

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

No comments:

Post a Comment

MCQ: Unit: 1 Contemporary Technology

 1) The network of physical devices which are built-in with sensors, hardware and software? a) AI b) VR c) IOT d) Cloud computing 2) Which o...