Está en la página 1de 3

ALT+F11

CLICK DERECHO… INSERTAR… MODULO

PEGAR EL SIGUIENTE CODIGO:


'FUNCION ALETRAS
Function ALETRAS(Numero As Double, Optional DecimalEnLetra As Boolean) As String

'Declaracion de variables

Dim Moneda As String


Dim Monedas As String
Dim Centavo As String
Dim Centavos As String
Dim Con As String
Dim NumCentavos As Double
Dim Letra As String
Const Maximo = 1999999999.99

' Parámetros
Moneda = "Peso"
Monedas = "Pesos"
Decimal1 = "Centavo"
Decimales = "Centavos"
Con = "Con"

'Si el numero esta dentro de 0 y 1999999999.99 (Maximo) entonces


'convertir el numero a letras

If (Numero >= 0) And (Numero <= Maximo) Then

Letra = LETRAS((Fix(Numero)))

'Si solo es un numero entonces


'agregar la moneda en singular
If (Numero = 1) Then
Letra = Letra & " " & Moneda
'De lo contrario agregar la moneda en plural
Else
Letra = Letra & " " & Monedas
End If

NumCentavos = Round((Numero - Fix(Numero)) * 100) 'Pasar a decimales (al


numero le resta el numero entero y lo multiplica por 100)

'Si los centvos son mayores a cero entonces


If NumCentavos >= 0 Then
'Si el parámetro DecimalEnLetra es VERDADERO
If DecimalEnLetra Then
'Convertir los centavos en letra
Letra = Letra & " " & Con & " " & LETRAS(Fix(NumCentavos))

'Si el centavo es uno agregar leyenda Centavo (Singular)


If (NumCentavos = 1) Then
Letra = Letra & " " & Decimal1
'De lo contrario agregar la leyenda Centavos (Plural)
Else
Letra = Letra & " " & Decimales
End If
'De lo contrario mostrar los centecimos como número
Else
'Si los centavos son menores a 10 entonces
If NumCentavos < 10 Then
Letra = Letra & " " & Con & " " & " 0" & NumCentavos & "/100"
Else
'De lo contrario
Letra = Letra & " " & Con & " " & NumCentavos & "/100"
End If
End If
End If

'Regresar el resultado final de la conversión


ALETRAS = Letra

Else
'Si el Numero no está dentro de los límites mostrar un mensaje de error
ALETRAS = "ERROR: el importe esta fuera del límite."
End If

End Function

'Funcion LETRAS
Function LETRAS(Numero As Long) As String

'Declaracion de las variables


Dim Unidades, Decenas, Centenas
Dim Resultado As String

'Numeros en letras
Unidades = Array("", "Un", "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete",
"Ocho", "Nueve", "Diez", "Once", "Doce", "Trece", "Catorce", "Quince",
"Dieciséis", "Diecisiete", "Dieciocho", "Diecinueve", "Veinte", "Veintiuno",
"Veintidos", "Veintitres", "Veinticuatro", "Veinticinco", "Veintiseis",
"Veintisiete", "Veintiocho", "Veintinueve")
Decenas = Array("", "Diez", "Veinte", "Treinta", "Cuarenta", "Cincuenta",
"Sesenta", "Setenta", "Ochenta", "Noventa", "Cien")
Centenas = Array("", "Ciento", "Doscientos", "Trescientos", "Cuatrocientos",
"Quinientos", "Seiscientos", "Setecientos", "Ochocientos", "Novecientos")

Select Case Numero


Case 0
Resultado = "Cero"
Case 1 To 29
Resultado = Unidades(Numero)
Case 30 To 100
Resultado = Decenas(Numero \ 10) + IIf(Numero Mod 10 <> 0, " y " +
LETRAS(Numero Mod 10), "")
Case 101 To 999
Resultado = Centenas(Numero \ 100) + IIf(Numero Mod 100 <> 0, " " +
LETRAS(Numero Mod 100), "")
Case 1000 To 1999
Resultado = "Mil" + IIf(Numero Mod 1000 <> 0, " " + LETRAS(Numero Mod
1000), "")
Case 2000 To 999999
Resultado = LETRAS(Numero \ 1000) + " Mil" + IIf(Numero Mod 1000 <> 0, "
" + LETRAS(Numero Mod 1000), "")
Case 1000000 To 1999999
Resultado = "Un Millón" + IIf(Numero Mod 1000000 <> 0, " " +
LETRAS(Numero Mod 1000000), "")
Case 2000000 To 1999999999
Resultado = LETRAS(Numero \ 1000000) + " Millones" + IIf(Numero Mod
1000000 <> 0, " " + LETRAS(Numero Mod 1000000), "")
End Select

LETRAS = Resultado

End Function

USAR FUNCION ALETRAS

También podría gustarte