Convertir numeros a letras en Excel (Macro) Función "=NumeroALetras"
' Función principal: recibe un número y devuelve su representación en letras.
Public Function NumeroALetras(ByVal numero As Double) As String
Dim entero As Long
entero = Int(numero)
NumeroALetras = ConvertirNumero(entero)
End Function
' Función que descompone el número en millones, miles y el resto (menor a 1000).
Public Function ConvertirNumero(ByVal numero As Long) As String
Dim texto As String
Dim millones As Long, miles As Long, resto As Long
If numero = 0 Then
ConvertirNumero = "cero"
Exit Function
End If
' Descomponer el número en millones, miles y el resto.
millones = numero \ 1000000
resto = numero Mod 1000000
miles = resto \ 1000
resto = resto Mod 1000
' Procesar millones
If millones > 0 Then
If millones = 1 Then
texto = texto & "un millón "
Else
texto = texto & ConvertirMenor1000(millones) & " millones "
End If
End If
' Procesar miles
If miles > 0 Then
If miles = 1 Then
texto = texto & "mil "
Else
texto = texto & ConvertirMenor1000(miles) & " mil "
End If
End If
' Procesar el resto (de 0 a 999)
If resto > 0 Then
texto = texto & ConvertirMenor1000(resto)
End If
ConvertirNumero = Trim(texto)
End Function
' Función que convierte un número de 1 a 999 en letras.
Public Function ConvertirMenor1000(ByVal numero As Long) As String
Dim texto As String
Dim centenas As Long, decenaUnidad As Long
' Caso especial: 100 exacto
If numero = 100 Then
ConvertirMenor1000 = "cien"
Exit Function
End If
' Procesar centenas
centenas = numero \ 100
Select Case centenas
Case 1
If numero Mod 100 = 0 Then
texto = "cien"
Else
texto = "ciento "
End If
Case 2: texto = "doscientos "
Case 3: texto = "trescientos "
Case 4: texto = "cuatrocientos "
Case 5: texto = "quinientos "
Case 6: texto = "seiscientos "
Case 7: texto = "setecientos "
Case 8: texto = "ochocientos "
Case 9: texto = "novecientos "
Case Else: texto = ""
End Select
' Procesar decenas y unidades (números entre 0 y 99)
decenaUnidad = numero Mod 100
If decenaUnidad < 30 Then
Select Case decenaUnidad
Case 0: ' no se agrega nada
Case 1: texto = texto & "uno"
Case 2: texto = texto & "dos"
Case 3: texto = texto & "tres"
Case 4: texto = texto & "cuatro"
Case 5: texto = texto & "cinco"
Case 6: texto = texto & "seis"
Case 7: texto = texto & "siete"
Case 8: texto = texto & "ocho"
Case 9: texto = texto & "nueve"
Case 10: texto = texto & "diez"
Case 11: texto = texto & "once"
Case 12: texto = texto & "doce"
Case 13: texto = texto & "trece"
Case 14: texto = texto & "catorce"
Case 15: texto = texto & "quince"
Case 16: texto = texto & "dieciséis"
Case 17: texto = texto & "diecisiete"
Case 18: texto = texto & "dieciocho"
Case 19: texto = texto & "diecinueve"
Case 20: texto = texto & "veinte"
Case 21: texto = texto & "veintiuno"
Case 22: texto = texto & "veintidós"
Case 23: texto = texto & "veintitrés"
Case 24: texto = texto & "veinticuatro"
Case 25: texto = texto & "veinticinco"
Case 26: texto = texto & "veintiséis"
Case 27: texto = texto & "veintisiete"
Case 28: texto = texto & "veintiocho"
Case 29: texto = texto & "veintinueve"
End Select
Else
Dim decenas As Long, unidades As Long
decenas = decenaUnidad \ 10
unidades = decenaUnidad Mod 10
Select Case decenas
Case 3: texto = texto & "treinta"
Case 4: texto = texto & "cuarenta"
Case 5: texto = texto & "cincuenta"
Case 6: texto = texto & "sesenta"
Case 7: texto = texto & "setenta"
Case 8: texto = texto & "ochenta"
Case 9: texto = texto & "noventa"
End Select
If unidades > 0 Then
texto = texto & " y "
Select Case unidades
Case 1: texto = texto & "uno"
Case 2: texto = texto & "dos"
Case 3: texto = texto & "tres"
Case 4: texto = texto & "cuatro"
Case 5: texto = texto & "cinco"
Case 6: texto = texto & "seis"
Case 7: texto = texto & "siete"
Case 8: texto = texto & "ocho"
Case 9: texto = texto & "nueve"
End Select
End If
End If
ConvertirMenor1000 = Trim(texto)
End Function
Comentarios
Publicar un comentario