La UDF NumeroATextoMoneda sirve para
convertir
números a textos en formato moneda (pesos mexicanos), ejemplo:
En la celda C1 tenemos la cantidad en número 3050.8, y en la celda C2 ingresamos =NumeroATextoMoneda(C1) y
obtendremos el siguiente resultado:
A continuación te dejo el código de la UDF para que la revises y la puedas usar o modificar.
Copiar código
Function NumeroATextoMoneda(Numero)
Application.Volatile
Dim texto
Dim Billones
Dim Millones
Dim Miles
Dim Cientos
Dim Decimales
Dim Cadena
Dim CadBillones
Dim CadMillones
Dim CadMiles
Dim CadCientos
texto = Numero
texto = FormatNumber(texto, 2)
texto = Right(Space(18) & texto, 18)
Billones = Mid(texto, 1, 3)
Millones = Mid(texto, 5, 3)
Miles = Mid(texto, 9, 3)
Cientos = Mid(texto, 13, 3)
Decimales = Mid(texto, 17, 2)
CadBillones = ConvierteCifra(Billones, 1)
CadMillones = ConvierteCifra(Millones, 1)
CadMiles = ConvierteCifra(Miles, 1)
CadCientos = ConvierteCifra(Cientos, 0)
If Trim(CadBillones) > "" Then
If Trim(CadBillones) = "UN" Then
Cadena = CadBillones & " BILLON"
Else
Cadena = CadBillones & " BILLONES"
End If
End If
If Trim(CadMillones) > "" Then
If Trim(CadMillones) = "UN" Then
Cadena = CadMillones & " MILLON"
Else
Cadena = CadMillones & " MILLONES"
End If
End If
If Trim(CadMiles) > "" Then
Cadena = Cadena & " " & CadMiles & " MIL"
End If
If Trim(CadMiles & CadCientos) = "UN0" Then
Cadena = Cadena & "UN PESO " & Decimales & "/100 M.N."
Else
If Miles & Cientos = "000000" Then
Cadena = Cadena & " " & Trim(CadCientos) & " PESOS " & Decimales & "/100 M.N."
Else
Cadena = Cadena & " " & Trim(CadCientos) & " PESOS " & Decimales & "/100 M.N. "
End If
End If
Select Case Numero
Case Is < 0
Cadena = "El valor es negativo"
Case Is > 999999999999.99
Cadena = "El valor excede el limite de la función"
Case Is < 1
Cadena = "CERO PESOS " & Decimales & "/100 M.N."
Case Is < 2
Cadena = "UN PESO " & Decimales & "/100 M.N."
End Select
NumeroATextoMoneda = Trim(Cadena)
End Function
Function ConvierteCifra(texto, SW)
Dim Centena
Dim Decena
Dim Unidad
Dim txtCentena
Dim txtDecena
Dim txtUnidad
Centena = Mid(texto, 1, 1)
Decena = Mid(texto, 2, 1)
Unidad = Mid(texto, 3, 1)
Select Case Centena
Case "1"
txtCentena = "CIEN"
If Decena & Unidad <> "00" Then
txtCentena = "CIENTO"
End If
Case "2"
txtCentena = "DOSCIENTOS"
Case "3"
txtCentena = "TRESCIENTOS"
Case "4"
txtCentena = "CUATROCIENTOS"
Case "5"
txtCentena = "QUINIENTOS"
Case "6"
txtCentena = "SEISCIENTOS"
Case "7"
txtCentena = "SETECIENTOS"
Case "8"
txtCentena = "OCHOCIENTOS"
Case "9"
txtCentena = "NOVECIENTOS"
End Select
Select Case Decena
Case "1"
txtDecena = "DIEZ"
Select Case Unidad
Case "1"
txtDecena = "ONCE"
Case "2"
txtDecena = "DOCE"
Case "3"
txtDecena = "TRECE"
Case "4"
txtDecena = "CATORCE"
Case "5"
txtDecena = "QUINCE"
Case "6"
txtDecena = "DIECISEIS"
Case "7"
txtDecena = "DIECISIETE"
Case "8"
txtDecena = "DIECIOCHO"
Case "9"
txtDecena = "DIECINUEVE"
End Select
Case "2"
txtDecena = "VEINTE"
If Unidad <> "0" Then
txtDecena = "VEINTI"
End If
Case "3"
txtDecena = "TREINTA"
If Unidad <> "0" Then
txtDecena = "TREINTA Y "
End If
Case "4"
txtDecena = "CUARENTA"
If Unidad <> "0" Then
txtDecena = "CUARENTA Y "
End If
Case "5"
txtDecena = "CINCUENTA"
If Unidad <> "0" Then
txtDecena = "CINCUENTA Y "
End If
Case "6"
txtDecena = "SESENTA"
If Unidad <> "0" Then
txtDecena = "SESENTA Y "
End If
Case "7"
txtDecena = "SETENTA"
If Unidad <> "0" Then
txtDecena = "SETENTA Y "
End If
Case "8"
txtDecena = "OCHENTA"
If Unidad <> "0" Then
txtDecena = "OCHENTA Y "
End If
Case "9"
txtDecena = "NOVENTA"
If Unidad <> "0" Then
txtDecena = "NOVENTA Y "
End If
End Select
If Decena <> "1" Then
Select Case Unidad
Case "1"
If SW Then
txtUnidad = "UN"
Else
txtUnidad = "UN"
End If
Case "2"
txtUnidad = "DOS"
Case "3"
txtUnidad = "TRES"
Case "4"
txtUnidad = "CUATRO"
Case "5"
txtUnidad = "CINCO"
Case "6"
txtUnidad = "SEIS"
Case "7"
txtUnidad = "SIETE"
Case "8"
txtUnidad = "OCHO"
Case "9"
txtUnidad = "NUEVE"
End Select
End If
ConvierteCifra = txtCentena & " " & txtDecena & txtUnidad
End Function