Vous êtes sur la page 1sur 9

'start excel 'press alt+ alt+f11 'insert menu and click module

excel number to words

Option Explicit 'Main Function Function SpellNumber( SpellNumber(ByVal MyNumber) MyNumber) Dim Dollars, Dollars, Cents, Cents, Temp Dim DecimalPlace, DecimalPlace, Count ReDim Place( Place(9) As String Place( Place(2) = " Thousand " Place( Place(3) = " Million " Place( Place(4) = " Billion " Place( Place(5) = " Trillion " ' String representation of amount. amount. MyNumber = Trim( Trim(Str( Str(MyNumber)) MyNumber)) ' Position of decimal place 0 if none. none. DecimalPlace = InStr( InStr(MyNumber, MyNumber, ".") ' Convert cents and set MyNumber to dollar amount. amount. If DecimalPlace > 0 Then Cents = GetTens( GetTens(Left( Left(Mid( Mid(MyNumber, MyNumber, DecimalPlace + 1) & _ "00 "00" 00", 2)) MyNumber = Trim( Trim(Left( Left(MyNumber, MyNumber, DecimalPlace - 1)) End If Count = 1 Do While MyNumber <> "" Temp = GetHundreds( GetHundreds(Right( Right(MyNumber, MyNumber, 3)) If Temp <> "" Then Dollars = Temp & Place( Place(Count) Count) & Dollars Page 1

excel number to words If Len( Len(MyNumber) MyNumber) > 3 Then MyNumber = Left( Left(MyNumber, MyNumber, Len( Len(MyNumber) MyNumber) - 3) Else MyNumber = "" End If Count = Count + 1 Loop Select Case Dollars Case "" Dollars = "No "No Dollars" Dollars" Case "One" One" Dollars = "One "One Dollar" Dollar" Case Else Dollars = Dollars & " Dollars" Dollars" End Select Select Case Cents Case "" Cents = " and No Cents" Cents" Case "One" One" Cents = " and One Cent" Cent" Case Else Cents = " and " & Cents & " Cents" Cents" End Select SpellNumber = Dollars & Cents End Function ' Converts a number from 100100-999 into text Function GetHundreds( GetHundreds(ByVal MyNumber) MyNumber) Dim Result As String If Val( Val(MyNumber) MyNumber) = 0 Then Exit Function MyNumber = Right(" Right("000 ("000" 000" & MyNumber, MyNumber, 3) ' Convert the hundreds place. place. If Mid( Mid(MyNumber, MyNumber, 1, 1) <> "0" Then Page 2

excel number to words Result = GetDigit( GetDigit(Mid( Mid(MyNumber, MyNumber, 1, 1)) & " Hundred " End If ' Convert the tens and ones place. place. If Mid( Mid(MyNumber, MyNumber, 2, 1) <> "0" Then Result = Result & GetTens( GetTens(Mid( Mid(MyNumber, MyNumber, 2)) Else Result = Result & GetDigit( GetDigit(Mid( Mid(MyNumber, MyNumber, 3)) End If GetHundreds = Result End Function ' Converts a number from 10 to 99 into text. text. Function GetTens( GetTens(TensText) TensText) Dim Result As String Result = "" ' Null out the temporary function value. value. If Val( ' If Val(Left( Left(TensText, TensText, 1)) = 1 Then value between 1010-19... 19... Select Case Val( Val(TensText) TensText) Case 10: 10: Result = "Ten "Ten" Ten" Case 11: 11: Result = "Eleven "Eleven" Eleven" Case 12: 12: Result = "Twelve "Twelve" Twelve" Case 13: 13: Result = "Thirteen "Thirteen" Thirteen" Case 14: 14: Result = "Fourteen "Fourteen" Fourteen" Case 15: 15: Result = "Fifteen "Fifteen" Fifteen" Case 16: 16: Result = "Sixteen "Sixteen" Sixteen" Case 17: 17: Result = "Seventeen "Seventeen" Seventeen" Case 18: 18: Result = "Eighteen "Eighteen" Eighteen" Case 19: 19: Result = "Nineteen "Nineteen" Nineteen" Case Else End Select Page 3

Else ' If value between 2020-99... 99... Select Case Val( Val(Left( Left(TensText, TensText, 1)) Case 2: Result = "Twenty "Twenty " Case 3: Result = "Thirty "Thirty " Case 4: Result = "Forty "Forty " Case 5: Result = "Fifty "Fifty " Case 6: Result = "Sixty "Sixty " Case 7: Result = "Seventy "Seventy " Case 8: Result = "Eighty "Eighty " Case 9: Result = "Ninety "Ninety " Case Else End Select Result = Result & GetDigit _ (Right (Right( Right(TensText, TensText, 1)) ' Retrieve ones place. place. End If GetTens = Result End Function ' Converts a number from 1 to 9 into text. text. Function GetDigit( GetDigit(Digit) Digit) Select Case Val( Val(Digit) Digit) Case 1: GetDigit = "One "One" One" Case 2: GetDigit = "Two "Two" Two" Case 3: GetDigit = "Three "Three" Three" Case 4: GetDigit = "Four "Four" Four" Case 5: GetDigit = "Five "Five" Five" Case 6: GetDigit = "Six "Six" Six" Case 7: GetDigit = "Seven "Seven" Seven" Case 8: GetDigit = "Eight "Eight" Eight" Case 9: GetDigit = "Nine "Nine" Nine" Case Else: Else: GetDigit = "" End Select Page 4

excel number to words

End Function ''-------------------------------------------''-----------------------------------------------------------------------------------------------------------'second code Formula is =WordNum( WordNum(value) value) Step1 Step1: press alt+ alt+F11 Step2 Step2: Insert Module Step3 Step3: type ='wordnum( wordnum(12) 12)' will show `Twelve' Twelve'.

excel number to words

Option Explicit Public Numbers As Variant, Variant, Tens As Variant Sub SetNums() SetNums() Numbers = Array("" Array("", ("", "One" One", "Two" Two", "Three" Three", "Four" Four", "Five" Five", "Six" Six", "Seven" Seven", "Eight" Eight", "Nine" Nine", "Ten" Ten", "Eleven" Eleven", "Twelve" Twelve", "Thirteen" Thirteen", "Fourteen" Fourteen", "Fifteen" Fifteen", "Sixteen" Sixteen", "Seventeen" Seventeen", "Eighteen" Eighteen", "Nineteen") Nineteen") Tens = Array("" Array("", ("", "", "", "Twenty" Twenty", "Thirty" Thirty", "Forty" Forty", "Fifty" Fifty", "Sixty" Sixty", "Seventy" Seventy", "Eighty" Eighty", "Ninety") Ninety") End Sub Function WordNum( WordNum(MyNumber As Double) Double) As String Dim DecimalPosition As Integer, Integer, ValNo As Variant, Variant, StrNo As String Dim NumStr As String, String, n As Integer, Integer, Temp1 Temp1 As Page 5

excel number to words String, String, Temp2 Temp2 As String ' This macro was written by Chris Mead www. www.MeadInKent. MeadInKent.co. co.uk If Abs( Abs(MyNumber) MyNumber) > 999999999 Then WordNum = "Value "Value too large" large" Exit Function End If SetNums ' String representation of amount (excl decimals) decimals) NumStr = Right(" Right("000000000 ("000000000" 000000000" & Trim( Trim(Str( Str(Int( Int(Abs( Abs(MyNumber)))) MyNumber)))), )))), 9) ValNo = Array( Array(0, Val( Val(Mid( Mid(NumStr, NumStr, 1, 3)), )), Val( Val(Mid( Mid(NumStr, NumStr, 4, 3)), )), Val( Val(Mid( Mid(NumStr, NumStr, 7, 3))) For n = 3 To 1 Step -1 'analyse the absolute number as 3 sets of 3 digits StrNo = Format( Format(ValNo( ValNo(n), "000") 000") If ValNo( ValNo(n) > 0 Then Temp1 Temp1 = GetTens( GetTens(Val( Val(Right( Right(StrNo, StrNo, 2))) If Left( Left(StrNo, StrNo, 1) <> "0" Then Temp2 Temp2 = Numbers( Numbers(Val( Val(Left( Left(StrNo, StrNo, 1))) & " hundred" hundred" If Temp1 Temp1 <> "" Then Temp2 Temp2 = Temp2 Temp2 & " and " Else Temp2 Temp2 = "" End If If n = 3 Then Page 6

excel number to words If Temp2 Temp2 = "" And ValNo( ValNo(1) + ValNo( ValNo(2) > 0 Then Temp2 Temp2 = "and "and " WordNum = Trim( Trim(Temp2 Temp2 & Temp1 Temp1) End If '-------------------------'third code Formula is =WordNum( WordNum(value) value) And in order to create this formula follow the below steps before using it. it. Step1 Step1: press alt+ alt+F11 Step2 Step2: MENU: MENU: Insert ? Module Step3 Step3: copy and paste the below code and save & close the module/ module/window. window. Step4 Step4: type =wordnum( wordnum(12) 12) will show Twelve. Twelve. Option Explicit Public Numbers As Variant, Variant, Tens As Variant Sub SetNums() SetNums() Numbers = Array("" Array("", ("", "One" One", "Two" Two", "Three" Three", "Four" Four", "Five" Five", "Six" Six", "Seven" Seven", "Eight" Eight", "Nine" Nine", "Ten" Ten", "Eleven" Eleven", "Twelve" Twelve", "Thirteen" Thirteen", "Fourteen" Fourteen", "Fifteen" Fifteen", "Sixteen" Sixteen", "Seventeen" Seventeen", "Eighteen" Eighteen", "Nineteen") Nineteen") Tens = Array("" Array("", ("", "", "", "Twenty" Twenty", "Thirty" Thirty", "Forty" Forty", "Fifty" Fifty", "Sixty" Sixty", "Seventy" Seventy", "Eighty" Eighty", "Ninety") Ninety") End Sub Function WordNum( WordNum(MyNumber As Double) Double) As String Dim DecimalPosition As Integer, Integer, ValNo As Variant, Variant, StrNo As String Page 7

excel number to words Dim NumStr As String, String, n As Integer, Integer, Temp1 Temp1 As String, String, Temp2 Temp2 As String ' This macro was written by Chris Mead www. www.MeadInKent. MeadInKent.co. co.uk If Abs( Abs(MyNumber) MyNumber) > 999999999 Then WordNum = "Value "Value too large" large" Exit Function End If SetNums ' String representation of amount (excl decimals) decimals) NumStr = Right(" Right("000000000 ("000000000" 000000000" & Trim( Trim(Str( Str(Int( Int(Abs( Abs(MyNumber)))) MyNumber)))), )))), 9) ValNo = Array( Array(0, Val( Val(Mid( Mid(NumStr, NumStr, 1, 3)), )), Val( Val(Mid( Mid(NumStr, NumStr, 4, 3)), )), Val( Val(Mid( Mid(NumStr, NumStr, 7, 3))) For n = 3 To 1 Step -1 'analyse the absolute number as 3 sets of 3 digits StrNo = Format( Format(ValNo( ValNo(n), "000") 000") If ValNo( ValNo(n) > 0 Then Temp1 Temp1 = GetTens( GetTens(Val( Val(Right( Right(StrNo, StrNo, 2))) If Left( Left(StrNo, StrNo, 1) <> "0" Then Temp2 Temp2 = Numbers( Numbers(Val( Val(Left( Left(StrNo, StrNo, 1))) & " hundred" hundred" If Temp1 Temp1 <> "" Then Temp2 Temp2 = Temp2 Temp2 & " and " Else Temp2 Temp2 = "" End If If n = 3 Then If Temp2 Temp2 = "" And ValNo( ValNo(1) + ValNo( ValNo(2) > 0 Then Temp2 Temp2 = "and "and " WordNum = Trim( Trim(Temp2 Temp2 & Temp1 Temp1) End If If n = 2 Then WordNum = Trim( Trim(Temp2 Temp2 & Temp1 Temp1 & " Page 8

excel number to words thousand " & WordNum) WordNum) If n = 1 Then WordNum = Trim( Trim(Temp2 Temp2 & Temp1 Temp1 & " million " & WordNum) WordNum) End If Next n NumStr = Trim( Trim(Str( Str(Abs( Abs(MyNumber))) MyNumber))) ' Values after the decimal place DecimalPosition = InStr( InStr(NumStr, NumStr, ".") Numbers( Numbers(0) = "Zero "Zero" Zero" If DecimalPosition > 0 And DecimalPosition < Len( Len(NumStr) NumStr) Then Temp1 Temp1 = " point" point" For n = DecimalPosition + 1 To Len( Len(NumStr) NumStr) Temp1 Temp1 = Temp1 Temp1 & " " & Numbers( Numbers(Val( Val(Mid( Mid(NumStr, NumStr, n, 1))) Next n WordNum = WordNum & Temp1 Temp1 End If If Len( Len(WordNum) WordNum) = 0 Or Left( Left(WordNum, WordNum, 2) = " p" Then WordNum = "Zero "Zero" Zero" & WordNum End If End Function Function GetTens( GetTens(TensNum As Integer) Integer) As String ' Converts a number from 0 to 99 into text. text. If TensNum <= 19 Then GetTens = Numbers( Numbers(TensNum) TensNum) Else Dim MyNo As String MyNo = Format( Format(TensNum, TensNum, "00") 00") GetTens = Tens( Tens(Val( Val(Left( Left(MyNo, MyNo, 1))) & " " & Numbers( Numbers(Val( Val(Right( Right(MyNo, MyNo, 1))) End If End Function Page 9