Help! Converting numbers from Userform into text?

A

Adrian.Harper

Hello,

After trapsing through the newsgroups & websites I cannot seem to find
an answer to my problem!

Basically, when a user enters numbers into a text box in my Userform, I
want VBA to take those numbers and insert them both as the numbers in 1
bookmark, and as text in another bookmark.

E.g. User enters "100" in the userform text box, which the Word
Document inputs "100" in 1 bookmark and "One Hundred" in another
bookmark.

Thank you to anyone who can provide the answer!

Adrian
 
J

Jonathan West

Hello,

After trapsing through the newsgroups & websites I cannot seem to find
an answer to my problem!

Basically, when a user enters numbers into a text box in my Userform, I
want VBA to take those numbers and insert them both as the numbers in 1
bookmark, and as text in another bookmark.

E.g. User enters "100" in the userform text box, which the Word
Document inputs "100" in 1 bookmark and "One Hundred" in another
bookmark.

Thank you to anyone who can provide the answer!

Adrian

http://vbnet.mvps.org/index.html?code/helpers/numbertotext.htm

the code is VB6 rather than VBA, but it is quite easy to adapt.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
A

Adrian.Harper

Sorry - I only have 'an advanced beginner' knowledge of VBA & Userform!

How would I code the Userform using that script? Where do i tell it to
look for a number in a text box and where do I tell it to put the text
into a bookmark?

Thanks again for your help - here's some of my Userform Code under the
OK button if it helps:

Dim strName As String

If cboEst.Value = "XXX" Then strName = "XXX"
If cboEst.Value = "YYY" Then strName = "YYY"

With ActiveDocument
.Bookmarks("bmkRefIni").Range.Text = strIni
.Bookmarks("bmkRefNum").Range.Text = txtTN.Value
.Bookmarks("bmkComp").Range.Text = txtCN.Value
.Bookmarks("bmkAdd").Range.Text = txtCA.Value
.Bookmarks("bmkFAO").Range.Text = txtCC.Value
.Bookmarks("bmkReSite").Range.Text = txtSN.Value
.Bookmarks("bmkPriceNum").Range.Text = txtPrice.Value
.Bookmarks("bmkSigName").Range.Text = strName
.Bookmarks("bmkSigPos").Range.Text = strPos
End With
 
J

Jonathan West

On the page I referred to, copy the code starting at "Private Sub
BuildArray(sNumberText() As String" to the end of the code area, and paste
it into the code area of your userform.

Now, to take the example from your code below, suppose you want the price as
a number to go into bmkPriceNum, and the price in words to go into
bmkPriceText, you would do the following

..Bookmarks("bmkPriceNum").Range.Text = txtPrice.Value
..Bookmarks("bmkPriceText").Range.Text = NumberAsText(txtPrice.Value)

I notice you are in the UK, so you may want to modify the code so that it
can give you an amount in pounds & pence rather than dollars & cents. I'll
leave that conversion to you as an exercise to help you move beyond
"advanced beginner" status :)

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jen

Hi,
Beginer needs help with this please.
I am not sure how to adapt. I setup Text1 Label1 Command1 and pasted code
behind Userform.
I am not sure about setting Control to 0 what is exact name of Control
Index? Could that cause the below error?

I get a Type Mismatch Error 13 at this point?

Private Sub Command1_Click()

Dim value As String

*** value = Text1(0).Text***

Should I be doing something different?
 
J

Jonathan West

Jen said:
Hi,
Beginer needs help with this please.
I am not sure how to adapt. I setup Text1 Label1 Command1 and pasted code
behind Userform.
I am not sure about setting Control to 0 what is exact name of Control
Index? Could that cause the below error?

I get a Type Mismatch Error 13 at this point?

Private Sub Command1_Click()

Dim value As String

*** value = Text1(0).Text***

Should I be doing something different?

You've been tripped up by the fact that VB6 forms are not quite the same as
VBA UserForms. Try this

value = Text1.Text


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
A

Adrian.Harper

Many thanks for your continued help with this Jonathan!

I think I am so close now - the wall is still in my path but I can see
the light!

I've slapped the code into the userform as per your instructions, but I
Userform is having a hissy-fit when trying to conver the numbers to
text as it goes down the script. Maybe its something to do with it
being VB6?

The error message reads: "Compile error: Variable not defined" and
highlights "snumbertext" within the brackets that immediately follow
"IsBounded" in the following part of the script:

'----------------------------------------
'Check/create array. If this is the first
'time using this routine, create the text
'strings that will be used.
'----------------------------------------
If Not IsBounded(sNumberText) Then
Call BuildArray(sNumberText)
End If

Here is how the script is looking in userform if it's of any further
use (PS - I've furthered my advanced beginner status you'll see ;)

Option Explicit

Private Sub BuildArray(sNumberText() As String)

Dim sNumberText(0 To 27) As String

sNumberText(0) = "Zero"
sNumberText(1) = "One"
sNumberText(2) = "Two"
sNumberText(3) = "Three"
sNumberText(4) = "Four"
sNumberText(5) = "Five"
sNumberText(6) = "Six"
sNumberText(7) = "Seven"
sNumberText(8) = "Eight"
sNumberText(9) = "Nine"
sNumberText(10) = "Ten"
sNumberText(11) = "Eleven"
sNumberText(12) = "Twelve"
sNumberText(13) = "Thirteen"
sNumberText(14) = "Fourteen"
sNumberText(15) = "Fifteen"
sNumberText(16) = "Sixteen"
sNumberText(17) = "Seventeen"
sNumberText(18) = "Eighteen"
sNumberText(19) = "Nineteen"
sNumberText(20) = "Twenty"
sNumberText(21) = "Thirty"
sNumberText(22) = "Forty"
sNumberText(23) = "Fifty"
sNumberText(24) = "Sixty"
sNumberText(25) = "Seventy"
sNumberText(26) = "Eighty"
sNumberText(27) = "Ninety"

End Sub


Private Function IsBounded(vntArray As Variant) As Boolean

'note: the application in the IDE will stop
'at this line when first run if the IDE error
'mode is not set to "Break on Unhandled Errors"
'(Tools/Options/General/Error Trapping)
On Error Resume Next
IsBounded = IsNumeric(UBound(vntArray))

End Function


Private Function HundredsTensUnits(ByVal TestValue As Integer, _
Optional bUseAnd As Boolean) As
String

Dim CardinalNumber As Integer

If TestValue > 99 Then
CardinalNumber = TestValue \ 100
HundredsTensUnits = sNumberText(CardinalNumber) & " Hundred "
TestValue = TestValue - (CardinalNumber * 100)
End If

If bUseAnd = True Then
HundredsTensUnits = HundredsTensUnits & "and "
End If

If TestValue > 20 Then
CardinalNumber = TestValue \ 10
HundredsTensUnits = HundredsTensUnits & _
sNumberText(CardinalNumber + 18) & " "
TestValue = TestValue - (CardinalNumber * 10)
End If

If TestValue > 0 Then
HundredsTensUnits = HundredsTensUnits & sNumberText(TestValue) &
" "
End If

End Function


Private Function NumberAsText(NumberIn As Variant, _
Optional AND_or_CHECK_or_POUND As String)
As String
Dim cnt As Long
Dim DecimalPoint As Long
Dim CardinalNumber As Long
Dim CommaAdjuster As Long
Dim TestValue As Long
Dim CurrValue As Currency
Dim CentsString As String
Dim NumberSign As String
Dim WholePart As String
Dim BigWholePart As String
Dim DecimalPart As String
Dim tmp As String
Dim sStyle As String
Dim bUseAnd As Boolean
Dim bUseCheck As Boolean
Dim bUsePounds As Boolean


'----------------------------------------
'Begin setting conditions for formatting
'----------------------------------------

'Determine whether to apply special formatting.
'If nothing passed, return routine result
'converted only into its numeric equivalents,
'with no additional format text.
sStyle = LCase(AND_or_CHECK_or_POUND)

'User passed "AND": "and" will be added
'between hundredths and tens of pounds,
'ie "Three Hundred and Forty Two"
bUseAnd = sStyle = "and"

'User passed "POUND": "pound(s)" and "pence"
'appended to string,
'ie "Three Hundred and Forty Two Pounds"
bUsePounds = sStyle = "pound"

'User passed "CHECK" *or* "DOLLAR"
'If "check", cent amount returned as a fraction /100
'i.e. "Three Hundred Forty Two and 00/100"
'If "pound" was passed, "pound(s)" and "pence"
'appended instead.
bUseCheck = (sStyle = "check") Or (sStyle = "pound")


'----------------------------------------
'Check/create array. If this is the first
'time using this routine, create the text
'strings that will be used.
'----------------------------------------
If Not IsBounded(sNumberText) Then
Call BuildArray(sNumberText)
End If

'----------------------------------------
'Begin validating the number, and breaking
'into constituent parts
'----------------------------------------

'prepare to check for valid value in
NumberIn = Trim$(NumberIn)

If Not IsNumeric(NumberIn) Then

'invalid entry - abort
NumberAsText = "Error - Number improperly formed"
Exit Function

Else

'decimal check
DecimalPoint = InStr(NumberIn, ".")

If DecimalPoint > 0 Then

'split the fractional and primary numbers
DecimalPart = Mid$(NumberIn, DecimalPoint + 1)
WholePart = Left$(NumberIn, DecimalPoint - 1)

Else

'assume the decimal is the last char
DecimalPoint = Len(NumberIn) + 1
WholePart = NumberIn

End If

If InStr(NumberIn, ",,") Or _
InStr(NumberIn, ",.") Or _
InStr(NumberIn, ".,") Or _
InStr(DecimalPart, ",") Then

NumberAsText = "Error - Improper use of commas"
Exit Function

ElseIf InStr(NumberIn, ",") Then

CommaAdjuster = 0
WholePart = ""

For cnt = DecimalPoint - 1 To 1 Step -1

If Not Mid$(NumberIn, cnt, 1) Like "[,]" Then

WholePart = Mid$(NumberIn, cnt, 1) & WholePart

Else

CommaAdjuster = CommaAdjuster + 1

If (DecimalPoint - cnt - CommaAdjuster) Mod 3 Then

NumberAsText = "Error - Improper use of commas"
Exit Function

End If 'If
End If 'If Not
Next 'For cnt
End If 'If InStr
End If 'If Not


If Left$(WholePart, 1) Like "[+-]" Then
NumberSign = IIf(Left$(WholePart, 1) = "-", "Minus ", "Plus ")
WholePart = Mid$(WholePart, 2)
End If


'----------------------------------------
'Begin code to assure decimal portion of
'check value is not inadvertently rounded
'----------------------------------------
If bUseCheck = True Then

CurrValue = CCur(Val("." & DecimalPart))
DecimalPart = Mid$(Format$(CurrValue, "0.00"), 3, 2)

If CurrValue >= 0.995 Then

If WholePart = String$(Len(WholePart), "9") Then

WholePart = "1" & String$(Len(WholePart), "0")

Else

For cnt = Len(WholePart) To 1 Step -1

If Mid$(WholePart, cnt, 1) = "9" Then
Mid$(WholePart, cnt, 1) = "0"
Else
Mid$(WholePart, cnt, 1) = CStr(Val(Mid$(WholePart,
cnt, 1)) + 1)
Exit For
End If

Next

End If 'If WholePart
End If 'If CurrValue
End If 'If bUseCheck

'----------------------------------------
'Final prep step - this assures number
'within range of formatting code below
'----------------------------------------
If Len(WholePart) > 9 Then
BigWholePart = Left$(WholePart, Len(WholePart) - 9)
WholePart = Right$(WholePart, 9)
End If

If Len(BigWholePart) > 9 Then

NumberAsText = "Error - Number too large"
Exit Function

ElseIf Not WholePart Like String$(Len(WholePart), "#") Or _
(Not BigWholePart Like String$(Len(BigWholePart), "#") _
And Len(BigWholePart) > 0) Then

NumberAsText = "Error - Number improperly formed"
Exit Function

End If

'----------------------------------------
'Begin creating the output string
'----------------------------------------

'Very Large values
TestValue = Val(BigWholePart)

If TestValue > 999999 Then
CardinalNumber = TestValue \ 1000000
tmp = HundredsTensUnits(CardinalNumber) & "Quadrillion "
TestValue = TestValue - (CardinalNumber * 1000000)
End If

If TestValue > 999 Then
CardinalNumber = TestValue \ 1000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Trillion "
TestValue = TestValue - (CardinalNumber * 1000)
End If

If TestValue > 0 Then
tmp = tmp & HundredsTensUnits(TestValue) & "Billion "
End If

'Lesser values
TestValue = Val(WholePart)

If TestValue = 0 And BigWholePart = "" Then tmp = "Zero "

If TestValue > 999999 Then
CardinalNumber = TestValue \ 1000000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Million "
TestValue = TestValue - (CardinalNumber * 1000000)
End If

If TestValue > 999 Then
CardinalNumber = TestValue \ 1000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Thousand "
TestValue = TestValue - (CardinalNumber * 1000)
End If

If TestValue > 0 Then
If Val(WholePart) < 99 And BigWholePart = "" Then bUseAnd = False
tmp = tmp & HundredsTensUnits(TestValue, bUseAnd)
End If

'If in dollar mode, assure the text is the correct plurality
If bUsePounds = True Then

PenceString = HundredsTensUnits(DecimalPart)

If tmp = "One " Then
tmp = tmp & "Pound"
Else
tmp = tmp & "Pounds"
End If

If Len(PenceString) > 0 Then

tmp = tmp & " and " & PenceString

If PenceString = "One " Then
tmp = tmp & "Cent"
Else
tmp = tmp & "Cents"
End If

End If

ElseIf bUseCheck = True Then

tmp = tmp & "and " & Left$(DecimalPart & "00", 2)
tmp = tmp & "/100"

Else

If Len(DecimalPart) > 0 Then

tmp = tmp & "Point"

For cnt = 1 To Len(DecimalPart)
tmp = tmp & " " & sNumberText(Mid$(DecimalPart, cnt, 1))
Next

End If 'If DecimalPart
End If 'If bUsePounds


'done!
NumberAsText = NumberSign & tmp

End Function

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
End Sub

Private Sub UserForm_Initialize()
 
A

Adrian.Harper

Please can anybody help - I still can't get to the bottom of this which
is doing my head in!!!


Many thanks for your continued help with this Jonathan!

I think I am so close now - the wall is still in my path but I can see
the light!

I've slapped the code into the userform as per your instructions, but I
Userform is having a hissy-fit when trying to conver the numbers to
text as it goes down the script. Maybe its something to do with it
being VB6?

The error message reads: "Compile error: Variable not defined" and
highlights "snumbertext" within the brackets that immediately follow
"IsBounded" in the following part of the script:

'----------------------------------------
'Check/create array. If this is the first
'time using this routine, create the text
'strings that will be used.
'----------------------------------------
If Not IsBounded(sNumberText) Then
Call BuildArray(sNumberText)
End If

Here is how the script is looking in userform if it's of any further
use (PS - I've furthered my advanced beginner status you'll see ;)

Option Explicit

Private Sub BuildArray(sNumberText() As String)

Dim sNumberText(0 To 27) As String

sNumberText(0) = "Zero"
sNumberText(1) = "One"
sNumberText(2) = "Two"
sNumberText(3) = "Three"
sNumberText(4) = "Four"
sNumberText(5) = "Five"
sNumberText(6) = "Six"
sNumberText(7) = "Seven"
sNumberText(8) = "Eight"
sNumberText(9) = "Nine"
sNumberText(10) = "Ten"
sNumberText(11) = "Eleven"
sNumberText(12) = "Twelve"
sNumberText(13) = "Thirteen"
sNumberText(14) = "Fourteen"
sNumberText(15) = "Fifteen"
sNumberText(16) = "Sixteen"
sNumberText(17) = "Seventeen"
sNumberText(18) = "Eighteen"
sNumberText(19) = "Nineteen"
sNumberText(20) = "Twenty"
sNumberText(21) = "Thirty"
sNumberText(22) = "Forty"
sNumberText(23) = "Fifty"
sNumberText(24) = "Sixty"
sNumberText(25) = "Seventy"
sNumberText(26) = "Eighty"
sNumberText(27) = "Ninety"

End Sub


Private Function IsBounded(vntArray As Variant) As Boolean

'note: the application in the IDE will stop
'at this line when first run if the IDE error
'mode is not set to "Break on Unhandled Errors"
'(Tools/Options/General/Error Trapping)
On Error Resume Next
IsBounded = IsNumeric(UBound(vntArray))

End Function


Private Function HundredsTensUnits(ByVal TestValue As Integer, _
Optional bUseAnd As Boolean) As
String

Dim CardinalNumber As Integer

If TestValue > 99 Then
CardinalNumber = TestValue \ 100
HundredsTensUnits = sNumberText(CardinalNumber) & " Hundred "
TestValue = TestValue - (CardinalNumber * 100)
End If

If bUseAnd = True Then
HundredsTensUnits = HundredsTensUnits & "and "
End If

If TestValue > 20 Then
CardinalNumber = TestValue \ 10
HundredsTensUnits = HundredsTensUnits & _
sNumberText(CardinalNumber + 18) & " "
TestValue = TestValue - (CardinalNumber * 10)
End If

If TestValue > 0 Then
HundredsTensUnits = HundredsTensUnits & sNumberText(TestValue) &
" "
End If

End Function


Private Function NumberAsText(NumberIn As Variant, _
Optional AND_or_CHECK_or_POUND As String)
As String
Dim cnt As Long
Dim DecimalPoint As Long
Dim CardinalNumber As Long
Dim CommaAdjuster As Long
Dim TestValue As Long
Dim CurrValue As Currency
Dim CentsString As String
Dim NumberSign As String
Dim WholePart As String
Dim BigWholePart As String
Dim DecimalPart As String
Dim tmp As String
Dim sStyle As String
Dim bUseAnd As Boolean
Dim bUseCheck As Boolean
Dim bUsePounds As Boolean


'----------------------------------------
'Begin setting conditions for formatting
'----------------------------------------

'Determine whether to apply special formatting.
'If nothing passed, return routine result
'converted only into its numeric equivalents,
'with no additional format text.
sStyle = LCase(AND_or_CHECK_or_POUND)

'User passed "AND": "and" will be added
'between hundredths and tens of pounds,
'ie "Three Hundred and Forty Two"
bUseAnd = sStyle = "and"

'User passed "POUND": "pound(s)" and "pence"
'appended to string,
'ie "Three Hundred and Forty Two Pounds"
bUsePounds = sStyle = "pound"

'User passed "CHECK" *or* "DOLLAR"
'If "check", cent amount returned as a fraction /100
'i.e. "Three Hundred Forty Two and 00/100"
'If "pound" was passed, "pound(s)" and "pence"
'appended instead.
bUseCheck = (sStyle = "check") Or (sStyle = "pound")


'----------------------------------------
'Check/create array. If this is the first
'time using this routine, create the text
'strings that will be used.
'----------------------------------------
If Not IsBounded(sNumberText) Then
Call BuildArray(sNumberText)
End If

'----------------------------------------
'Begin validating the number, and breaking
'into constituent parts
'----------------------------------------

'prepare to check for valid value in
NumberIn = Trim$(NumberIn)

If Not IsNumeric(NumberIn) Then

'invalid entry - abort
NumberAsText = "Error - Number improperly formed"
Exit Function

Else

'decimal check
DecimalPoint = InStr(NumberIn, ".")

If DecimalPoint > 0 Then

'split the fractional and primary numbers
DecimalPart = Mid$(NumberIn, DecimalPoint + 1)
WholePart = Left$(NumberIn, DecimalPoint - 1)

Else

'assume the decimal is the last char
DecimalPoint = Len(NumberIn) + 1
WholePart = NumberIn

End If

If InStr(NumberIn, ",,") Or _
InStr(NumberIn, ",.") Or _
InStr(NumberIn, ".,") Or _
InStr(DecimalPart, ",") Then

NumberAsText = "Error - Improper use of commas"
Exit Function

ElseIf InStr(NumberIn, ",") Then

CommaAdjuster = 0
WholePart = ""

For cnt = DecimalPoint - 1 To 1 Step -1

If Not Mid$(NumberIn, cnt, 1) Like "[,]" Then

WholePart = Mid$(NumberIn, cnt, 1) & WholePart

Else

CommaAdjuster = CommaAdjuster + 1

If (DecimalPoint - cnt - CommaAdjuster) Mod 3 Then

NumberAsText = "Error - Improper use of commas"
Exit Function

End If 'If
End If 'If Not
Next 'For cnt
End If 'If InStr
End If 'If Not


If Left$(WholePart, 1) Like "[+-]" Then
NumberSign = IIf(Left$(WholePart, 1) = "-", "Minus ", "Plus ")
WholePart = Mid$(WholePart, 2)
End If


'----------------------------------------
'Begin code to assure decimal portion of
'check value is not inadvertently rounded
'----------------------------------------
If bUseCheck = True Then

CurrValue = CCur(Val("." & DecimalPart))
DecimalPart = Mid$(Format$(CurrValue, "0.00"), 3, 2)

If CurrValue >= 0.995 Then

If WholePart = String$(Len(WholePart), "9") Then

WholePart = "1" & String$(Len(WholePart), "0")

Else

For cnt = Len(WholePart) To 1 Step -1

If Mid$(WholePart, cnt, 1) = "9" Then
Mid$(WholePart, cnt, 1) = "0"
Else
Mid$(WholePart, cnt, 1) = CStr(Val(Mid$(WholePart,
cnt, 1)) + 1)
Exit For
End If

Next

End If 'If WholePart
End If 'If CurrValue
End If 'If bUseCheck

'----------------------------------------
'Final prep step - this assures number
'within range of formatting code below
'----------------------------------------
If Len(WholePart) > 9 Then
BigWholePart = Left$(WholePart, Len(WholePart) - 9)
WholePart = Right$(WholePart, 9)
End If

If Len(BigWholePart) > 9 Then

NumberAsText = "Error - Number too large"
Exit Function

ElseIf Not WholePart Like String$(Len(WholePart), "#") Or _
(Not BigWholePart Like String$(Len(BigWholePart), "#") _
And Len(BigWholePart) > 0) Then

NumberAsText = "Error - Number improperly formed"
Exit Function

End If

'----------------------------------------
'Begin creating the output string
'----------------------------------------

'Very Large values
TestValue = Val(BigWholePart)

If TestValue > 999999 Then
CardinalNumber = TestValue \ 1000000
tmp = HundredsTensUnits(CardinalNumber) & "Quadrillion "
TestValue = TestValue - (CardinalNumber * 1000000)
End If

If TestValue > 999 Then
CardinalNumber = TestValue \ 1000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Trillion "
TestValue = TestValue - (CardinalNumber * 1000)
End If

If TestValue > 0 Then
tmp = tmp & HundredsTensUnits(TestValue) & "Billion "
End If

'Lesser values
TestValue = Val(WholePart)

If TestValue = 0 And BigWholePart = "" Then tmp = "Zero "

If TestValue > 999999 Then
CardinalNumber = TestValue \ 1000000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Million "
TestValue = TestValue - (CardinalNumber * 1000000)
End If

If TestValue > 999 Then
CardinalNumber = TestValue \ 1000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Thousand "
TestValue = TestValue - (CardinalNumber * 1000)
End If

If TestValue > 0 Then
If Val(WholePart) < 99 And BigWholePart = "" Then bUseAnd = False
tmp = tmp & HundredsTensUnits(TestValue, bUseAnd)
End If

'If in dollar mode, assure the text is the correct plurality
If bUsePounds = True Then

PenceString = HundredsTensUnits(DecimalPart)

If tmp = "One " Then
tmp = tmp & "Pound"
Else
tmp = tmp & "Pounds"
End If

If Len(PenceString) > 0 Then

tmp = tmp & " and " & PenceString

If PenceString = "One " Then
tmp = tmp & "Cent"
Else
tmp = tmp & "Cents"
End If

End If

ElseIf bUseCheck = True Then

tmp = tmp & "and " & Left$(DecimalPart & "00", 2)
tmp = tmp & "/100"

Else

If Len(DecimalPart) > 0 Then

tmp = tmp & "Point"

For cnt = 1 To Len(DecimalPart)
tmp = tmp & " " & sNumberText(Mid$(DecimalPart, cnt, 1))
Next

End If 'If DecimalPart
End If 'If bUsePounds


'done!
NumberAsText = NumberSign & tmp

End Function

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
End Sub

Private Sub UserForm_Initialize()





Jonathan said:
You've been tripped up by the fact that VB6 forms are not quite the same as
VBA UserForms. Try this

value = Text1.Text


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

Doug Robbins - Word MVP

The following converts a number entered into the textbox txtnum into text
when the command button is clicked

Private Sub CommandButton1_Click()
Dim myNumber
Dim numbertext As String
If IsNumeric(txtnum.Text) Then
myNumber = Val(txtnum.Text)
Else
MsgBox "You must enter only numerals.)"
Exit Sub
End If
numbertext = ConvertCurrencyToEnglish(ByVal myNumber)
Msgbox numbertext
End Sub

Function ConvertCurrencyToEnglish(ByVal myNumber)
Dim Temp
Dim Dollars, Cents
Dim DecimalPlace, Count


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

myNumber = Trim(Str(myNumber))

DecimalPlace = InStr(myNumber, ".")
If DecimalPlace > 0 Then
Temp = Left(Mid(myNumber, DecimalPlace + 1) & "00", 2)
Cents = ConvertTens(Temp)
myNumber = Trim(Left(myNumber, DecimalPlace - 1))
End If
Count = 1
Do While myNumber <> ""
'convert last 3 digits to English Dollars
Temp = ConvertHundreds(Right(myNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(myNumber) > 3 Then
'remove last 3 comverted digits
myNumber = Left(myNumber, Len(myNumber) - 3)
Else
myNumber = ""
End If
Count = Count + 1
Loop

'clean up dollars
Select Case Dollars
Case ""
Dollars = "NoDollars"
Case "One"
Dollars = "One Dollar"
Case Else
Dollars = Dollars & " Dollars"
End Select

'clean up cents
Select Case Cents
Case ""
Cents = " And No Cents"
Case "One"
Cents = " And One Cent"
Case Else
Cents = " And " & Cents & " Cents"
End Select
ConvertCurrencyToEnglish = Dollars & Cents
End Function
Private Function ConvertHundreds(ByVal myNumber)
Dim Result As String
If Val(myNumber) = 0 Then Exit Function

'append leading zeros to number
myNumber = Right("000" & myNumber, 3)

'do we have hundreds place digit to convert?
If Left(myNumber, 1) <> "0" Then
Result = ConvertDigit(Left(myNumber, 1)) & " Hundred "
End If

'do we have tens place digit to convert?
If Mid(myNumber, 2, 1) <> "0" Then
Result = Result & ConvertTens(Mid(myNumber, 2))
Else
'if not, then convert the ones place digit
Result = Result & ConvertDigit(Mid(myNumber, 3))
End If
ConvertHundreds = Trim(Result)
End Function
Private Function ConvertTens(ByVal MyTens)
Dim Result As String
'is value between 10 and 19?
If Val(Left(MyTens, 1)) = 1 Then
Select Case Val(MyTens)
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
Select Case Val(Left(MyTens, 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

'convert ones place digit
Result = Result & ConvertDigit(Right(MyTens, 1))
End If
ConvertTens = Result
End Function
Private Function ConvertDigit(ByVal MyDigit)
Select Case Val(MyDigit)
Case 1: ConvertDigit = "One"
Case 2: ConvertDigit = "Two"
Case 3: ConvertDigit = "Three"
Case 4: ConvertDigit = "Four"
Case 5: ConvertDigit = "Five"
Case 6: ConvertDigit = "Six"
Case 7: ConvertDigit = "Seven"
Case 8: ConvertDigit = "Eight"
Case 9: ConvertDigit = "Nine"
Case Else: ConvertDigit = ""
End Select
End Function


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Please can anybody help - I still can't get to the bottom of this which
is doing my head in!!!


Many thanks for your continued help with this Jonathan!

I think I am so close now - the wall is still in my path but I can see
the light!

I've slapped the code into the userform as per your instructions, but I
Userform is having a hissy-fit when trying to conver the numbers to
text as it goes down the script. Maybe its something to do with it
being VB6?

The error message reads: "Compile error: Variable not defined" and
highlights "snumbertext" within the brackets that immediately follow
"IsBounded" in the following part of the script:

'----------------------------------------
'Check/create array. If this is the first
'time using this routine, create the text
'strings that will be used.
'----------------------------------------
If Not IsBounded(sNumberText) Then
Call BuildArray(sNumberText)
End If

Here is how the script is looking in userform if it's of any further
use (PS - I've furthered my advanced beginner status you'll see ;)

Option Explicit

Private Sub BuildArray(sNumberText() As String)

Dim sNumberText(0 To 27) As String

sNumberText(0) = "Zero"
sNumberText(1) = "One"
sNumberText(2) = "Two"
sNumberText(3) = "Three"
sNumberText(4) = "Four"
sNumberText(5) = "Five"
sNumberText(6) = "Six"
sNumberText(7) = "Seven"
sNumberText(8) = "Eight"
sNumberText(9) = "Nine"
sNumberText(10) = "Ten"
sNumberText(11) = "Eleven"
sNumberText(12) = "Twelve"
sNumberText(13) = "Thirteen"
sNumberText(14) = "Fourteen"
sNumberText(15) = "Fifteen"
sNumberText(16) = "Sixteen"
sNumberText(17) = "Seventeen"
sNumberText(18) = "Eighteen"
sNumberText(19) = "Nineteen"
sNumberText(20) = "Twenty"
sNumberText(21) = "Thirty"
sNumberText(22) = "Forty"
sNumberText(23) = "Fifty"
sNumberText(24) = "Sixty"
sNumberText(25) = "Seventy"
sNumberText(26) = "Eighty"
sNumberText(27) = "Ninety"

End Sub


Private Function IsBounded(vntArray As Variant) As Boolean

'note: the application in the IDE will stop
'at this line when first run if the IDE error
'mode is not set to "Break on Unhandled Errors"
'(Tools/Options/General/Error Trapping)
On Error Resume Next
IsBounded = IsNumeric(UBound(vntArray))

End Function


Private Function HundredsTensUnits(ByVal TestValue As Integer, _
Optional bUseAnd As Boolean) As
String

Dim CardinalNumber As Integer

If TestValue > 99 Then
CardinalNumber = TestValue \ 100
HundredsTensUnits = sNumberText(CardinalNumber) & " Hundred "
TestValue = TestValue - (CardinalNumber * 100)
End If

If bUseAnd = True Then
HundredsTensUnits = HundredsTensUnits & "and "
End If

If TestValue > 20 Then
CardinalNumber = TestValue \ 10
HundredsTensUnits = HundredsTensUnits & _
sNumberText(CardinalNumber + 18) & " "
TestValue = TestValue - (CardinalNumber * 10)
End If

If TestValue > 0 Then
HundredsTensUnits = HundredsTensUnits & sNumberText(TestValue) &
" "
End If

End Function


Private Function NumberAsText(NumberIn As Variant, _
Optional AND_or_CHECK_or_POUND As String)
As String
Dim cnt As Long
Dim DecimalPoint As Long
Dim CardinalNumber As Long
Dim CommaAdjuster As Long
Dim TestValue As Long
Dim CurrValue As Currency
Dim CentsString As String
Dim NumberSign As String
Dim WholePart As String
Dim BigWholePart As String
Dim DecimalPart As String
Dim tmp As String
Dim sStyle As String
Dim bUseAnd As Boolean
Dim bUseCheck As Boolean
Dim bUsePounds As Boolean


'----------------------------------------
'Begin setting conditions for formatting
'----------------------------------------

'Determine whether to apply special formatting.
'If nothing passed, return routine result
'converted only into its numeric equivalents,
'with no additional format text.
sStyle = LCase(AND_or_CHECK_or_POUND)

'User passed "AND": "and" will be added
'between hundredths and tens of pounds,
'ie "Three Hundred and Forty Two"
bUseAnd = sStyle = "and"

'User passed "POUND": "pound(s)" and "pence"
'appended to string,
'ie "Three Hundred and Forty Two Pounds"
bUsePounds = sStyle = "pound"

'User passed "CHECK" *or* "DOLLAR"
'If "check", cent amount returned as a fraction /100
'i.e. "Three Hundred Forty Two and 00/100"
'If "pound" was passed, "pound(s)" and "pence"
'appended instead.
bUseCheck = (sStyle = "check") Or (sStyle = "pound")


'----------------------------------------
'Check/create array. If this is the first
'time using this routine, create the text
'strings that will be used.
'----------------------------------------
If Not IsBounded(sNumberText) Then
Call BuildArray(sNumberText)
End If

'----------------------------------------
'Begin validating the number, and breaking
'into constituent parts
'----------------------------------------

'prepare to check for valid value in
NumberIn = Trim$(NumberIn)

If Not IsNumeric(NumberIn) Then

'invalid entry - abort
NumberAsText = "Error - Number improperly formed"
Exit Function

Else

'decimal check
DecimalPoint = InStr(NumberIn, ".")

If DecimalPoint > 0 Then

'split the fractional and primary numbers
DecimalPart = Mid$(NumberIn, DecimalPoint + 1)
WholePart = Left$(NumberIn, DecimalPoint - 1)

Else

'assume the decimal is the last char
DecimalPoint = Len(NumberIn) + 1
WholePart = NumberIn

End If

If InStr(NumberIn, ",,") Or _
InStr(NumberIn, ",.") Or _
InStr(NumberIn, ".,") Or _
InStr(DecimalPart, ",") Then

NumberAsText = "Error - Improper use of commas"
Exit Function

ElseIf InStr(NumberIn, ",") Then

CommaAdjuster = 0
WholePart = ""

For cnt = DecimalPoint - 1 To 1 Step -1

If Not Mid$(NumberIn, cnt, 1) Like "[,]" Then

WholePart = Mid$(NumberIn, cnt, 1) & WholePart

Else

CommaAdjuster = CommaAdjuster + 1

If (DecimalPoint - cnt - CommaAdjuster) Mod 3 Then

NumberAsText = "Error - Improper use of commas"
Exit Function

End If 'If
End If 'If Not
Next 'For cnt
End If 'If InStr
End If 'If Not


If Left$(WholePart, 1) Like "[+-]" Then
NumberSign = IIf(Left$(WholePart, 1) = "-", "Minus ", "Plus ")
WholePart = Mid$(WholePart, 2)
End If


'----------------------------------------
'Begin code to assure decimal portion of
'check value is not inadvertently rounded
'----------------------------------------
If bUseCheck = True Then

CurrValue = CCur(Val("." & DecimalPart))
DecimalPart = Mid$(Format$(CurrValue, "0.00"), 3, 2)

If CurrValue >= 0.995 Then

If WholePart = String$(Len(WholePart), "9") Then

WholePart = "1" & String$(Len(WholePart), "0")

Else

For cnt = Len(WholePart) To 1 Step -1

If Mid$(WholePart, cnt, 1) = "9" Then
Mid$(WholePart, cnt, 1) = "0"
Else
Mid$(WholePart, cnt, 1) = CStr(Val(Mid$(WholePart,
cnt, 1)) + 1)
Exit For
End If

Next

End If 'If WholePart
End If 'If CurrValue
End If 'If bUseCheck

'----------------------------------------
'Final prep step - this assures number
'within range of formatting code below
'----------------------------------------
If Len(WholePart) > 9 Then
BigWholePart = Left$(WholePart, Len(WholePart) - 9)
WholePart = Right$(WholePart, 9)
End If

If Len(BigWholePart) > 9 Then

NumberAsText = "Error - Number too large"
Exit Function

ElseIf Not WholePart Like String$(Len(WholePart), "#") Or _
(Not BigWholePart Like String$(Len(BigWholePart), "#") _
And Len(BigWholePart) > 0) Then

NumberAsText = "Error - Number improperly formed"
Exit Function

End If

'----------------------------------------
'Begin creating the output string
'----------------------------------------

'Very Large values
TestValue = Val(BigWholePart)

If TestValue > 999999 Then
CardinalNumber = TestValue \ 1000000
tmp = HundredsTensUnits(CardinalNumber) & "Quadrillion "
TestValue = TestValue - (CardinalNumber * 1000000)
End If

If TestValue > 999 Then
CardinalNumber = TestValue \ 1000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Trillion "
TestValue = TestValue - (CardinalNumber * 1000)
End If

If TestValue > 0 Then
tmp = tmp & HundredsTensUnits(TestValue) & "Billion "
End If

'Lesser values
TestValue = Val(WholePart)

If TestValue = 0 And BigWholePart = "" Then tmp = "Zero "

If TestValue > 999999 Then
CardinalNumber = TestValue \ 1000000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Million "
TestValue = TestValue - (CardinalNumber * 1000000)
End If

If TestValue > 999 Then
CardinalNumber = TestValue \ 1000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Thousand "
TestValue = TestValue - (CardinalNumber * 1000)
End If

If TestValue > 0 Then
If Val(WholePart) < 99 And BigWholePart = "" Then bUseAnd = False
tmp = tmp & HundredsTensUnits(TestValue, bUseAnd)
End If

'If in dollar mode, assure the text is the correct plurality
If bUsePounds = True Then

PenceString = HundredsTensUnits(DecimalPart)

If tmp = "One " Then
tmp = tmp & "Pound"
Else
tmp = tmp & "Pounds"
End If

If Len(PenceString) > 0 Then

tmp = tmp & " and " & PenceString

If PenceString = "One " Then
tmp = tmp & "Cent"
Else
tmp = tmp & "Cents"
End If

End If

ElseIf bUseCheck = True Then

tmp = tmp & "and " & Left$(DecimalPart & "00", 2)
tmp = tmp & "/100"

Else

If Len(DecimalPart) > 0 Then

tmp = tmp & "Point"

For cnt = 1 To Len(DecimalPart)
tmp = tmp & " " & sNumberText(Mid$(DecimalPart, cnt, 1))
Next

End If 'If DecimalPart
End If 'If bUsePounds


'done!
NumberAsText = NumberSign & tmp

End Function

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
End Sub

Private Sub UserForm_Initialize()





Jonathan said:
Hi,
Beginer needs help with this please.
I am not sure how to adapt. I setup Text1 Label1 Command1 and pasted
code
behind Userform.
I am not sure about setting Control to 0 what is exact name of
Control
Index? Could that cause the below error?

I get a Type Mismatch Error 13 at this point?

Private Sub Command1_Click()

Dim value As String

*** value = Text1(0).Text***

Should I be doing something different?

You've been tripped up by the fact that VB6 forms are not quite the
same as
VBA UserForms. Try this

value = Text1.Text


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
A

Adrian.Harper

Thank you very much for that Doug - just had to change to Pound
Sterling Currency & away it goes!

Thanks again!

The following converts a number entered into the textbox txtnum into text
when the command button is clicked

Private Sub CommandButton1_Click()
Dim myNumber
Dim numbertext As String
If IsNumeric(txtnum.Text) Then
myNumber = Val(txtnum.Text)
Else
MsgBox "You must enter only numerals.)"
Exit Sub
End If
numbertext = ConvertCurrencyToEnglish(ByVal myNumber)
Msgbox numbertext
End Sub

Function ConvertCurrencyToEnglish(ByVal myNumber)
Dim Temp
Dim Dollars, Cents
Dim DecimalPlace, Count


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

myNumber = Trim(Str(myNumber))

DecimalPlace = InStr(myNumber, ".")
If DecimalPlace > 0 Then
Temp = Left(Mid(myNumber, DecimalPlace + 1) & "00", 2)
Cents = ConvertTens(Temp)
myNumber = Trim(Left(myNumber, DecimalPlace - 1))
End If
Count = 1
Do While myNumber <> ""
'convert last 3 digits to English Dollars
Temp = ConvertHundreds(Right(myNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(myNumber) > 3 Then
'remove last 3 comverted digits
myNumber = Left(myNumber, Len(myNumber) - 3)
Else
myNumber = ""
End If
Count = Count + 1
Loop

'clean up dollars
Select Case Dollars
Case ""
Dollars = "NoDollars"
Case "One"
Dollars = "One Dollar"
Case Else
Dollars = Dollars & " Dollars"
End Select

'clean up cents
Select Case Cents
Case ""
Cents = " And No Cents"
Case "One"
Cents = " And One Cent"
Case Else
Cents = " And " & Cents & " Cents"
End Select
ConvertCurrencyToEnglish = Dollars & Cents
End Function
Private Function ConvertHundreds(ByVal myNumber)
Dim Result As String
If Val(myNumber) = 0 Then Exit Function

'append leading zeros to number
myNumber = Right("000" & myNumber, 3)

'do we have hundreds place digit to convert?
If Left(myNumber, 1) <> "0" Then
Result = ConvertDigit(Left(myNumber, 1)) & " Hundred "
End If

'do we have tens place digit to convert?
If Mid(myNumber, 2, 1) <> "0" Then
Result = Result & ConvertTens(Mid(myNumber, 2))
Else
'if not, then convert the ones place digit
Result = Result & ConvertDigit(Mid(myNumber, 3))
End If
ConvertHundreds = Trim(Result)
End Function
Private Function ConvertTens(ByVal MyTens)
Dim Result As String
'is value between 10 and 19?
If Val(Left(MyTens, 1)) = 1 Then
Select Case Val(MyTens)
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
Select Case Val(Left(MyTens, 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

'convert ones place digit
Result = Result & ConvertDigit(Right(MyTens, 1))
End If
ConvertTens = Result
End Function
Private Function ConvertDigit(ByVal MyDigit)
Select Case Val(MyDigit)
Case 1: ConvertDigit = "One"
Case 2: ConvertDigit = "Two"
Case 3: ConvertDigit = "Three"
Case 4: ConvertDigit = "Four"
Case 5: ConvertDigit = "Five"
Case 6: ConvertDigit = "Six"
Case 7: ConvertDigit = "Seven"
Case 8: ConvertDigit = "Eight"
Case 9: ConvertDigit = "Nine"
Case Else: ConvertDigit = ""
End Select
End Function


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Please can anybody help - I still can't get to the bottom of this which
is doing my head in!!!


Many thanks for your continued help with this Jonathan!

I think I am so close now - the wall is still in my path but I can see
the light!

I've slapped the code into the userform as per your instructions, but I
Userform is having a hissy-fit when trying to conver the numbers to
text as it goes down the script. Maybe its something to do with it
being VB6?

The error message reads: "Compile error: Variable not defined" and
highlights "snumbertext" within the brackets that immediately follow
"IsBounded" in the following part of the script:

'----------------------------------------
'Check/create array. If this is the first
'time using this routine, create the text
'strings that will be used.
'----------------------------------------
If Not IsBounded(sNumberText) Then
Call BuildArray(sNumberText)
End If

Here is how the script is looking in userform if it's of any further
use (PS - I've furthered my advanced beginner status you'll see ;)

Option Explicit

Private Sub BuildArray(sNumberText() As String)

Dim sNumberText(0 To 27) As String

sNumberText(0) = "Zero"
sNumberText(1) = "One"
sNumberText(2) = "Two"
sNumberText(3) = "Three"
sNumberText(4) = "Four"
sNumberText(5) = "Five"
sNumberText(6) = "Six"
sNumberText(7) = "Seven"
sNumberText(8) = "Eight"
sNumberText(9) = "Nine"
sNumberText(10) = "Ten"
sNumberText(11) = "Eleven"
sNumberText(12) = "Twelve"
sNumberText(13) = "Thirteen"
sNumberText(14) = "Fourteen"
sNumberText(15) = "Fifteen"
sNumberText(16) = "Sixteen"
sNumberText(17) = "Seventeen"
sNumberText(18) = "Eighteen"
sNumberText(19) = "Nineteen"
sNumberText(20) = "Twenty"
sNumberText(21) = "Thirty"
sNumberText(22) = "Forty"
sNumberText(23) = "Fifty"
sNumberText(24) = "Sixty"
sNumberText(25) = "Seventy"
sNumberText(26) = "Eighty"
sNumberText(27) = "Ninety"

End Sub


Private Function IsBounded(vntArray As Variant) As Boolean

'note: the application in the IDE will stop
'at this line when first run if the IDE error
'mode is not set to "Break on Unhandled Errors"
'(Tools/Options/General/Error Trapping)
On Error Resume Next
IsBounded = IsNumeric(UBound(vntArray))

End Function


Private Function HundredsTensUnits(ByVal TestValue As Integer, _
Optional bUseAnd As Boolean) As
String

Dim CardinalNumber As Integer

If TestValue > 99 Then
CardinalNumber = TestValue \ 100
HundredsTensUnits = sNumberText(CardinalNumber) & " Hundred "
TestValue = TestValue - (CardinalNumber * 100)
End If

If bUseAnd = True Then
HundredsTensUnits = HundredsTensUnits & "and "
End If

If TestValue > 20 Then
CardinalNumber = TestValue \ 10
HundredsTensUnits = HundredsTensUnits & _
sNumberText(CardinalNumber + 18) & " "
TestValue = TestValue - (CardinalNumber * 10)
End If

If TestValue > 0 Then
HundredsTensUnits = HundredsTensUnits & sNumberText(TestValue) &
" "
End If

End Function


Private Function NumberAsText(NumberIn As Variant, _
Optional AND_or_CHECK_or_POUND As String)
As String
Dim cnt As Long
Dim DecimalPoint As Long
Dim CardinalNumber As Long
Dim CommaAdjuster As Long
Dim TestValue As Long
Dim CurrValue As Currency
Dim CentsString As String
Dim NumberSign As String
Dim WholePart As String
Dim BigWholePart As String
Dim DecimalPart As String
Dim tmp As String
Dim sStyle As String
Dim bUseAnd As Boolean
Dim bUseCheck As Boolean
Dim bUsePounds As Boolean


'----------------------------------------
'Begin setting conditions for formatting
'----------------------------------------

'Determine whether to apply special formatting.
'If nothing passed, return routine result
'converted only into its numeric equivalents,
'with no additional format text.
sStyle = LCase(AND_or_CHECK_or_POUND)

'User passed "AND": "and" will be added
'between hundredths and tens of pounds,
'ie "Three Hundred and Forty Two"
bUseAnd = sStyle = "and"

'User passed "POUND": "pound(s)" and "pence"
'appended to string,
'ie "Three Hundred and Forty Two Pounds"
bUsePounds = sStyle = "pound"

'User passed "CHECK" *or* "DOLLAR"
'If "check", cent amount returned as a fraction /100
'i.e. "Three Hundred Forty Two and 00/100"
'If "pound" was passed, "pound(s)" and "pence"
'appended instead.
bUseCheck = (sStyle = "check") Or (sStyle = "pound")


'----------------------------------------
'Check/create array. If this is the first
'time using this routine, create the text
'strings that will be used.
'----------------------------------------
If Not IsBounded(sNumberText) Then
Call BuildArray(sNumberText)
End If

'----------------------------------------
'Begin validating the number, and breaking
'into constituent parts
'----------------------------------------

'prepare to check for valid value in
NumberIn = Trim$(NumberIn)

If Not IsNumeric(NumberIn) Then

'invalid entry - abort
NumberAsText = "Error - Number improperly formed"
Exit Function

Else

'decimal check
DecimalPoint = InStr(NumberIn, ".")

If DecimalPoint > 0 Then

'split the fractional and primary numbers
DecimalPart = Mid$(NumberIn, DecimalPoint + 1)
WholePart = Left$(NumberIn, DecimalPoint - 1)

Else

'assume the decimal is the last char
DecimalPoint = Len(NumberIn) + 1
WholePart = NumberIn

End If

If InStr(NumberIn, ",,") Or _
InStr(NumberIn, ",.") Or _
InStr(NumberIn, ".,") Or _
InStr(DecimalPart, ",") Then

NumberAsText = "Error - Improper use of commas"
Exit Function

ElseIf InStr(NumberIn, ",") Then

CommaAdjuster = 0
WholePart = ""

For cnt = DecimalPoint - 1 To 1 Step -1

If Not Mid$(NumberIn, cnt, 1) Like "[,]" Then

WholePart = Mid$(NumberIn, cnt, 1) & WholePart

Else

CommaAdjuster = CommaAdjuster + 1

If (DecimalPoint - cnt - CommaAdjuster) Mod 3 Then

NumberAsText = "Error - Improper use of commas"
Exit Function

End If 'If
End If 'If Not
Next 'For cnt
End If 'If InStr
End If 'If Not


If Left$(WholePart, 1) Like "[+-]" Then
NumberSign = IIf(Left$(WholePart, 1) = "-", "Minus ", "Plus ")
WholePart = Mid$(WholePart, 2)
End If


'----------------------------------------
'Begin code to assure decimal portion of
'check value is not inadvertently rounded
'----------------------------------------
If bUseCheck = True Then

CurrValue = CCur(Val("." & DecimalPart))
DecimalPart = Mid$(Format$(CurrValue, "0.00"), 3, 2)

If CurrValue >= 0.995 Then

If WholePart = String$(Len(WholePart), "9") Then

WholePart = "1" & String$(Len(WholePart), "0")

Else

For cnt = Len(WholePart) To 1 Step -1

If Mid$(WholePart, cnt, 1) = "9" Then
Mid$(WholePart, cnt, 1) = "0"
Else
Mid$(WholePart, cnt, 1) = CStr(Val(Mid$(WholePart,
cnt, 1)) + 1)
Exit For
End If

Next

End If 'If WholePart
End If 'If CurrValue
End If 'If bUseCheck

'----------------------------------------
'Final prep step - this assures number
'within range of formatting code below
'----------------------------------------
If Len(WholePart) > 9 Then
BigWholePart = Left$(WholePart, Len(WholePart) - 9)
WholePart = Right$(WholePart, 9)
End If

If Len(BigWholePart) > 9 Then

NumberAsText = "Error - Number too large"
Exit Function

ElseIf Not WholePart Like String$(Len(WholePart), "#") Or _
(Not BigWholePart Like String$(Len(BigWholePart), "#") _
And Len(BigWholePart) > 0) Then

NumberAsText = "Error - Number improperly formed"
Exit Function

End If

'----------------------------------------
'Begin creating the output string
'----------------------------------------

'Very Large values
TestValue = Val(BigWholePart)

If TestValue > 999999 Then
CardinalNumber = TestValue \ 1000000
tmp = HundredsTensUnits(CardinalNumber) & "Quadrillion "
TestValue = TestValue - (CardinalNumber * 1000000)
End If

If TestValue > 999 Then
CardinalNumber = TestValue \ 1000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Trillion "
TestValue = TestValue - (CardinalNumber * 1000)
End If

If TestValue > 0 Then
tmp = tmp & HundredsTensUnits(TestValue) & "Billion "
End If

'Lesser values
TestValue = Val(WholePart)

If TestValue = 0 And BigWholePart = "" Then tmp = "Zero "

If TestValue > 999999 Then
CardinalNumber = TestValue \ 1000000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Million "
TestValue = TestValue - (CardinalNumber * 1000000)
End If

If TestValue > 999 Then
CardinalNumber = TestValue \ 1000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Thousand "
TestValue = TestValue - (CardinalNumber * 1000)
End If

If TestValue > 0 Then
If Val(WholePart) < 99 And BigWholePart = "" Then bUseAnd = False
tmp = tmp & HundredsTensUnits(TestValue, bUseAnd)
End If

'If in dollar mode, assure the text is the correct plurality
If bUsePounds = True Then

PenceString = HundredsTensUnits(DecimalPart)

If tmp = "One " Then
tmp = tmp & "Pound"
Else
tmp = tmp & "Pounds"
End If

If Len(PenceString) > 0 Then

tmp = tmp & " and " & PenceString

If PenceString = "One " Then
tmp = tmp & "Cent"
Else
tmp = tmp & "Cents"
End If

End If

ElseIf bUseCheck = True Then

tmp = tmp & "and " & Left$(DecimalPart & "00", 2)
tmp = tmp & "/100"

Else

If Len(DecimalPart) > 0 Then

tmp = tmp & "Point"

For cnt = 1 To Len(DecimalPart)
tmp = tmp & " " & sNumberText(Mid$(DecimalPart, cnt, 1))
Next

End If 'If DecimalPart
End If 'If bUsePounds


'done!
NumberAsText = NumberSign & tmp

End Function

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
End Sub

Private Sub UserForm_Initialize()





Jonathan West wrote:

Hi,
Beginer needs help with this please.
I am not sure how to adapt. I setup Text1 Label1 Command1 and pasted
code
behind Userform.
I am not sure about setting Control to 0 what is exact name of
Control
Index? Could that cause the below error?

I get a Type Mismatch Error 13 at this point?

Private Sub Command1_Click()

Dim value As String

*** value = Text1(0).Text***

Should I be doing something different?

You've been tripped up by the fact that VB6 forms are not quite the
same as
VBA UserForms. Try this

value = Text1.Text


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top