Una familia de software de hoja de cálculo de Microsoft con herramientas para analizar, trazar y comunicar datos
Hecho con Copilot.
' hecho con Copilot
' correcciones a mano:
' corregit uno mil
' corregir uno millón
' borrar el primer millones cuando hay dos
Function NumeroALetras2(ByVal MyNumber As String, _
Optional Separador As String = ".", _
Optional Moneda As String = "euros", _
Optional Fraccion As String = "céntimos") As String
Dim Pesos As String
Dim Centavos As String
Dim Temp As String
Dim DecimalPlace As Integer
Dim Count As Integer
Dim Place()
Dim Palabras() As String
Dim cnt As Integer
Dim i As Integer
ReDim Place(9)
Place(2) = " mil "
Place(3) = " millones "
Place(4) = " mil millones "
Place(5) = " billones "
' Convertir número a texto
MyNumber = Trim(Str(MyNumber))
' Buscar punto decimal
DecimalPlace = InStr(MyNumber, Separador)
If DecimalPlace > 0 Then
Centavos = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then
Pesos = Temp & Place(Count) & Pesos
End If
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
'para corregir el uno mil
If Count = 3 And Temp = "uno" Then
Pesos = Mid(Pesos, 4)
End If
'para corregir el uno millones
If Count = 4 And Temp = "uno" Then
Pesos = "un millon " & Mid(Pesos, 13)
End If
'para corregir el uno billones
If Count = 6 And Temp = "uno" Then
Pesos = "un billón " & Mid(Pesos, 13)
End If
Palabras = Split(Pesos, " ")
' borrar el primer millones cuando hay dos
' Contar cuántas veces aparece "millones"
For i = LBound(Palabras) To UBound(Palabras)
If Trim(Palabras(i)) = "millones" Then
cnt = cnt + 1
End If
Next i
If cnt > 1 Then
Pesos = Application.WorksheetFunction.Substitute(Pesos, "millones ", "", 1)
End If
NumeroALetras2 = Application.WorksheetFunction.Trim(Pesos) & " " & Moneda
If Centavos <> "" Then
NumeroALetras2 = Application.WorksheetFunction.Proper(NumeroALetras2 & " con " & Centavos & " " & Fraccion)
End If
End Function
Private Function GetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then
Exit Function
End If
MyNumber = Right("000" & MyNumber, 3)
If Mid(MyNumber, 1, 1) <> "0" Then
Select Case Mid(MyNumber, 1, 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 "
End Select
End If
If Mid(MyNumber, 2, 2) <> "00" Then
Result = Result & GetTens(Mid(MyNumber, 2))
End If
GetHundreds = Result
End Function
Private Function GetTens(TensText)
Dim Result As String
If Val(Left(TensText, 1)) = 1 Then
Select Case Val(TensText)
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 Else
Result = "dieci" & GetDigit(Right(TensText, 1))
End Select
ElseIf Val(Left(TensText, 1)) = 2 Then
If Right(TensText, 1) = "0" Then
Result = "veinte"
Else
Result = "veinti" & GetDigit(Right(TensText, 1))
End If
Else
Select Case Left(TensText, 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"
End Select
If Right(TensText, 1) <> "0" Then
If Result = "" Then
Result = GetDigit(Right(TensText, 1))
Else
Result = Result & " y " & GetDigit(Right(TensText, 1))
End If
End If
End If
GetTens = Result
End Function
Private Function GetDigit(Digit)
Select Case Val(Digit)
Case 1
GetDigit = "uno"
Case 2
GetDigit = "dos"
Case 3
GetDigit = "tres"
Case 4
GetDigit = "cuatro"
Case 5
GetDigit = "cinco"
Case 6
GetDigit = "seis"
Case 7
GetDigit = "siete"
Case 8
GetDigit = "ocho"
Case 9
GetDigit = "nueve"
Case Else
GetDigit = ""
End Select
End Function