Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

function

Posted on 2011-02-10
7
Medium Priority
?
237 Views
Last Modified: 2012-08-14
Experts,

I have copied a function from the internet but when I paste into Excel it gives me an error.
The debugger highlights "NUMBER"  not sure what is wrong here...
Thank you

Option Explicit

Function Convert Number To Spanish (ByVal MyNumber)
Dim Temp
Dim Dollars, Cents
Dim DecimalPlace, Count

ReDim Place(9) As String
Place(2) = " mil "
Place(3) = " millones "
Place(4) = " billones "
Place(5) = " trillones "

' Convert MyNumber to a string, trimming extra spaces.
MyNumber = Trim(Str(MyNumber))

' Find decimal place.
DecimalPlace = InStr(MyNumber, ".")

' If we find decimal place...
If DecimalPlace > 0 Then
' Convert cents
Temp = Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2)
Cents = ConvertTens(Temp)

' Strip off cents from remainder to convert.
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If

Count = 1
Do While MyNumber <> ""
' Convert last 3 digits of MyNumber to English dollars.
Temp = ConvertHundreds(Right(MyNumber, 3))
If Val(Right(MyNumber, 3)) = 1 Then
Select Case Count
Case 1: Dollars = "uno " & Dollars
Case 2: Dollars = "mil " & Dollars
Case 3: Dollars = "un millón " & Dollars
Case 4: Dollars = "un billón " & Dollars
Case 5: Dollars = "un trillón " & Dollars
Case Else: Dollars = Dollars
End Select
Else
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
End If

If Len(MyNumber) > 3 Then
' Remove last 3 converted digits from MyNumber.
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop

Dollars = Trim(Dollars)

' Clean up dollars.
If Dollars = "" Then Dollars = "cero"

Convert Number To Spanish = Dollars
End Function

Private Function ConvertHundreds(ByVal MyNumber)
Dim Result As String

' Exit if there is nothing to convert.
If Val(MyNumber) = 0 Then Exit Function

' Append leading zeros to number.
MyNumber = Right("000" & MyNumber, 3)

' Do we have a hundreds place digit to convert?
If Left(MyNumber, 1) <> "0" Then
Select Case Left(MyNumber, 1)
Case 1: Result = "ciento "
Case 2: Result = "doscientos "
Case 3: Result = "trescientos "
Case 4: Result = "cuatrocientos "
Case 5: Result = "quinientos "
Case 6: Result = "seiscientos "
Case 7: Result = "setecientos "
Case 8: Result = "ochocientos "
Case 9: Result = "novecientos "
Case Else
End Select
End If

' Do we have a 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

' is the number exactly 100? If so, overwrite
If Left(MyNumber, 3) = "100" Then
Result = "cien "
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 Or Val(Left(MyTens, 1)) = 2 Then
Select Case Val(MyTens)
Case 10: Result = "diez"
Case 11: Result = "once"
Case 12: Result = "doce"
Case 13: Result = "trece"
Case 14: Result = "catorce"
Case 15: Result = "quince"
Case 16: Result = "dieciséis"
Case 17: Result = "diecisiete"
Case 18: Result = "dieciocho"
Case 19: Result = "diecinueve"
Case 20: Result = "veinte"
Case 21: Result = "veintiuno"
Case 22: Result = "veintidós"
Case 23: Result = "veintitrés"
Case 24: Result = "veinticuatro"
Case 25: Result = "veinticinco"
Case 26: Result = "veintiséis"
Case 27: Result = "veintisiete"
Case 28: Result = "veintiocho"
Case 29: Result = "veintinueve"
Case Else
End Select
Else
' .. otherwise it's between 20 and 99.
Select Case Val(Left(MyTens, 1))
Case 3: Result = "treinta "
Case 4: Result = "cuarenta "
Case 5: Result = "cincuenta "
Case 6: Result = "sesenta "
Case 7: Result = "setenta "
Case 8: Result = "ochenta "
Case 9: Result = "noventa "
Case Else
End Select

' Convert ones place digit.
Result = Result & IIf(Trim(ConvertDigit(Right(MyTens, 1))) = "", "", "y ") & ConvertDigit(Right(MyTens, 1))
End If

ConvertTens = Result
End Function

Private Function ConvertDigit(ByVal MyDigit)
Select Case Val(MyDigit)
Case 1: ConvertDigit = "uno"
Case 2: ConvertDigit = "dos"
Case 3: ConvertDigit = "tres"
Case 4: ConvertDigit = "cuatro"
Case 5: ConvertDigit = "cinco"
Case 6: ConvertDigit = "seis"
Case 7: ConvertDigit = "siete"
Case 8: ConvertDigit = "ocho"
Case 9: ConvertDigit = "nueve"
Case Else: ConvertDigit = ""
End Select
End Function
0
Comment
Question by:pdvsa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 

Author Comment

by:pdvsa
ID: 34864853
the "number" found here:
Function Convert Number To Spanish (ByVal MyNumber)
0
 
LVL 24

Accepted Solution

by:
StephenJR earned 1600 total points
ID: 34864932
I don't think you can have spaces in function names, so ...

Function Convert_Number_To_Spanish (ByVal MyNumber)
0
 
LVL 26

Assisted Solution

by:pony10us
pony10us earned 400 total points
ID: 34864955
Function Convert Number To Spanish (ByVal MyNumber)

This line creates a Function called Convert and then looks for arguments within parantheses. Therefore the word Number is the first word that is invalid. Try the following:

Function ConvertNumberToSpanish (ByVal MyNumber)

Leaving the spaces out between the words in the name of the function. If this is referenced from somewhere else in the spreadsheet you will need to adjust those locations as well.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:pdvsa
ID: 34865117
OK it was the spaces.  But now I cant figure out how to run it.  

I thought it was a Macro but I inserted it as a Module and not sure how to run it.  

I have attached it so you can test it to see if I have done something incorrect.  


thank you
MODULE.xls
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 34865142
Just type this in any cell,

=Convert_Number_To_Spanish(A1)

You need to change the name by removing the spaces in the penultimate line as well.
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 34865153
Of course I don't know if the answer is right, but it does come up with one!
0
 

Author Closing Comment

by:pdvsa
ID: 34865246
Thank you.  I had to convert the name elsewhere as pony said...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question