Вы находитесь на странице: 1из 5

EJERCICIO FUNCIONES DEFINIDAS POR EL USUARIO (UDF)

En este ejemplo veremos como crear nuestras propias funciones definidas por el usuario
Para ello crearemos una funcion que convierta la cantidad numerica en letras.

El codigo presentado a continuacion sirve para convertir a texto en dolares


Una vez realizado, has los cambios necesarios para que conviertas en pesos.

1. Ve a la hoja llamada CODIGO (Hoja3) y observa y copia la funcion.


2. Abre el editor de Visual Basic de Excel y crea un nuevo modulo
3. Ahí teclearas el codigo
4. Graba y regresa a la hoja Ejemplo (Hoja2) para que hagas un ejemplo de tu nueva funcion

Por: Lic. Sergio Hugo Sanchez O.


IDSystems
Actualizacion: Octubre 2009
www.idsystemsmx.com
Para probar la nueva funcion que acabamos de crear, en la celda inferior teclea
la cantidad numerica que requieres, y luego en la fila siguiente, la funcion de conversion

Cantidad 145
En texto #VALUE!

100 100
Function OneDigit(Num As Long) As String
Dim Words(0 To 9) As String
Words(0) = "Zero"
Words(1) = "One"
Words(2) = "Two"
Words(3) = "Three"
Words(4) = "Four"
Words(5) = "Five"
Words(6) = "Six"
Words(7) = "Seven"
Words(8) = "Eight"
Words(9) = "Nine"
OneDigit = Words(Num)
End Function

Function TwoDigit(Num As Long) As String


Dim WordsBy10(2 To 9) As String
Dim Ones As Long, Tens As Long
Dim OnesString As String
Dim TensString As String
Ones = Num Mod 10
Tens = Int(Num / 10)
If Tens = 0 Or Tens > 1 Then
OnesString = OneDigit(Ones)
If Tens = 0 Then
TwoDigit = OnesString
Exit Function
End If
Else
Select Case Ones
Case 0
TwoDigit = "Ten"
Exit Function
Case 1
TwoDigit = "Eleven"
Exit Function
Case 2
TwoDigit = "Twelve"
Exit Function
Case 3
TwoDigit = "Thirteen"
Exit Function
Case 4
TwoDigit = "Fourteen"
Exit Function
Case 5
TwoDigit = "Fifteen"
Exit Function
Case 6
TwoDigit = "Sixteen"
Exit Function
Case 7
TwoDigit = "Seventeen"
Exit Function
Case 8
TwoDigit = "Eighteen"
Exit Function
Case 9
TwoDigit = "Nineteen"
Exit Function
End Select
End If
WordsBy10(2) = "Twenty"
WordsBy10(3) = "Thirty"
WordsBy10(4) = "Forty"
WordsBy10(5) = "Fifty"
WordsBy10(6) = "Sixty"
WordsBy10(7) = "Seventy"
WordsBy10(8) = "Eighty"
WordsBy10(9) = "Ninety"
If Ones > 0 Then
WordsBy10(Tens) = WordsBy10(Tens) & "-"
ElseIf Tens >= 2 Then
OnesString = ""
End If
If Tens >= 2 Then
TensString = WordsBy10(Tens)
Else
TensString = ""
End If
TwoDigit = TensString + OnesString
End Function

Function ThreeDigit(Num As Long) As String


Dim Remainder As Long, Hundreds As Long
Dim HundredsString As String, OnesString As String
Remainder = Num Mod 100
Hundreds = Int(Num / 100)
OnesString = TwoDigit(Remainder)
HundredsString = OneDigit(Hundreds) + " Hundred"
If Hundreds = 0 Then HundredsString = ""
If Remainder = 0 And Hundreds > 0 Then OnesString = ""
If Remainder > 0 And Hundreds > 0 Then HundredsString = HundredsString + " "
ThreeDigit = HundredsString + OnesString
End Function

Function SixDigit(Num As Long) As String


Dim Remainder As Long, Thousands As Long
Dim OnesString As String, ThousandsString As String
Remainder = Num Mod 1000
Thousands = Int(Num / 1000)
OnesString = ThreeDigit(Remainder)
ThousandsString = ThreeDigit(Thousands) + " Thousand"
If Remainder = 0 And Thousands > 0 Then OnesString = ""
If Remainder > 0 And Thousands > 0 Then ThousandsString = ThousandsString + " "
If Thousands = 0 Then ThousandsString = ""
SixDigit = ThousandsString + OnesString
End Function

Function GetCents(Num As Double) As String


Cents = (Num * 100) Mod 100
StrCents = Str(Cents)
If Cents < 10 Then StrCents = " 0" + Right(StrCents, Len(StrCents) - 1)
GetCents = StrCents + "/100"
End Function

Function Combine(Num As Double) As String


DollarsString = LTrim(SixDigit(Int(Num)))
CentsString = GetCents(Num)
Combine = DollarsString + " and" + CentsString
End Function

Function CheckText(Num As Double, NumChars As Integer) As String


CombineText = Combine(Num)
TextLength = Len(CombineText)
NumStars = NumChars - TextLength
CheckText = CombineText + String(NumStars, "*")
End Function

Вам также может понравиться