Skip to main content

Number to Text in Excel in currency format that spells out the decimal portion

follow the instructions on my previous post.
replace the code with the following:

Option Explicit
'****************
' Main Function *
'****************

Function SpellNumber(ByVal MyNumber)
Dim Temp, WholeNumberText, DecimalText
Dim DecimalPlace, Count

ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "

' String representation of amount.
MyNumber = Trim(Str(MyNumber))

' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
DecimalText = GetTens(Round(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2), 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If

Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then
WholeNumberText = Temp & Place(Count) & WholeNumberText
End If
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop

Select Case DecimalText
Case ""
DecimalText = " and 0/100"
Case Else
DecimalText = " and " & DecimalText & " cents"

End Select
SpellNumber = WholeNumberText & " dollars " & DecimalText
End Function


'*******************************************
' Converts a number from 100-999 into text *
'*******************************************
Function GetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)
' Convert the hundreds place.
If Mid(MyNumber, 1, 1) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If
' Convert the tens and ones place.
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

'*********************************************
' 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"
Case 16: Result = "Sixteen"
Case 17: Result = "Seventeen"
Case 18: Result = "Eighteen"
Case 19: Result = "Nineteen"
Case Else
End Select
Else ' If value between 20-99...
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 "
Case Else
End Select
Result = Result & GetDigit(Right(TensText, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function


'*******************************************
' Converts a number from 1 to 9 into text. *
'*******************************************

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

Comments

icedgurl said…
hi! i just added your link to my blog. :-)

Popular posts from this blog

How to register a business name

Attending business summits and conferences is a big help to those who belong to the quite "techy" (technological or technical) industry. Being a graduate of one, I had less knowledge in the field of entrepreneurship. Enrolling myself in business administration gave me quite the knowledge to be a part of the business world and thus improve my entrepreneurial skills. I now would like to share this information that I got familiar with (and I managed to get a copy of the entire process from the 6th Mindanao ICT Congress): How to Register a Business Name (in the Philippines) ----------------------------------------------------------------------------------------------- SINGLE PROPRIETORSHIP Applicant must secure 2 copies of registration form and pay Php 300.00 (rate may change) for single proprietorship registration processing fee. The registration shall be valid for five (5) years. A surcharge of Php 100.00 is imposed if renewal is filed beyond the three (3) month grace period, c

Adding a Footer to the DataGridView component

I have been searching for sites and forums that would give me a any hint on having a footer on the .net DataGridView control. It was frustrating. I found some, but not what I was looking for. I use windows forms. It would have been easier if I was into web. I decided to create one for myself. It's not complete, but it works with me. It needs improvement and I hope that some programmers who might pass through this blog will help me with it :D. Limitations: Cannot set Footer values during design time. Can sometimes hide a row when scrolled to the last item in the grid. What I did was just create a user control that inherits the DataGridView control and add a StatusStrip to act as the footer. public partial class MyDataGridView : DataGridView { public StatusStrip Footer { get { return (StatusStrip)this.Controls["Footer"]; } } private bool _footerVisible; [Browsable(false)] /// /// Sets or Gets the va

Using Firebird with ASP.net

In my previous post , I was able to connect a firebird 2.0 database to asp.net using controls. The next goal was to connect to a firebird database using the firebird.net engine and using the repeater control. I however, was able to use the firebird.net engine to connect to the firebird database but not to the repeater control. I used the html table tag. I've created a simple asp.net demo displaying the contents of a firebird database on page load. Here are the steps: 1. Things Needed: Firebird 2.0 Server Firebird 2.0 Client Visual Web Developer 2. In Visual Web Developer, create a new ASP.net Website (using C# as the programming language). The project will have a default page named Default.aspx. Make sure that the code is separate from the page (ex. Default.aspx.cs is separate from Default.aspx) 3. On the Website menu, click on Add Reference. 4. Select the Firebird - ADO.net 2.0 Data Provider and click Ok. 5. Add using FirebirdSql.Data.FirebirdClient; to the "using" sec