Excel VBA – Escrevendo números por extenso

Usando-se o Excel / VBA seria possível escrever-se uma função que após ler um numero na forma tradicional, o converta para forma “por extenso” ? Essa função em outras palavras, deverá ser capaz de ler um valor qualquer inteiro e escreve-lo por extenso (ex:”Quatro Mil Duzentos e Quinze”). Isso é possível ? Ficamos gratos pelo Help.

Public Function EscrevePorExtenso(ByVal n As Double) As String
    Unid = Array("", "Um", "Dois", "Três", "Quatro", "Cinco", _
                 "Seis", "Sete", "Oito", "Nove", "Dez", "Onze", "Doze", _
                 "Treze", "Quatorze", "Quinze", "Dezesseis", "Dezessete", _
                 "Dezoito", "Dezenove", "Vinte")
    Dezen = Array("", "Dez", "Vinte", "Trinta", "Quarenta", _
                  "Cinquenta", "Sessenta", "Setenta", "Oitenta", "Noventa")
    Centen = Array("", "Cento", "Duzentos", "Trezentos", _
                   "Quatrocentos", "Quinhentos", "Seiscentos", _
                   "Setecentos", "Oitocentos", "Novecentos", "Mil")
    Num = n
    Escr = ""
    If n = 0 Then
        Escr = "Zero"
    End If
    If (n \ 1000) > 0 And n \ 1000 < 10 Then         Escr = Unid(n \ 1000) & " Mil "     End If     n = n - (n \ 1000) * 1000     If n > 100 Then
        Escr = Escr & Centen(n \ 100)
    End If
    If n = 100 Then
        Escr = Escr & " Cem"
        GoTo Prossiga
    End If
    n = n - (n \ 100) * 100
    If n >= 20 And n < 100 Then         Escr = Escr & " " & Dezen(n \ 10)     End If     If n > 0 And n < 20 Then         Escr = Escr & " " & Unid(n)         GoTo Prossiga     End If     n = n - (n \ 10) * 10     If n > 0 Then
        Escr = Escr & " " & Unid(n)
    End If
Prossiga:
    If Num Mod 10 <> 0 Then
        If InStr(1, Escr, "Vinte", 1) = 0 Then
            If InStr(1, Escr, "Trinta", 1) = 0 Then
                If InStr(1, Escr, "enta", 1) > 0 Then
                    Escr = Application.Substitute(Escr, "enta", "enta e ")
                End If
            End If
        End If
    End If
    If Num Mod 10 <> 0 Then
        If InStr(1, Escr, "Vinte", 1) > 0 Then
            If InStr(1, Escr, "Trinta", 1) = 0 Then
                If InStr(1, Escr, "enta", 1) = 0 Then
                    Escr = Application.Substitute(Escr, "Vinte", "Vinte e ")
                End If
            End If
        End If
    End If
    If Num Mod 10 <> 0 Then
        If InStr(1, Escr, "Vinte", 1) = 0 Then
            If InStr(1, Escr, "Trinta", 1) > 0 Then
                If InStr(1, Escr, "enta", 1) = 0 Then
                    Escr = Application.Substitute(Escr, "Trinta", "Trinta e ")
                End If
            End If
        End If
    End If
    If Num Mod 100 <> 0 Then
        If InStr(1, Escr, "ento", 1) > 0 Then
            Escr = Application.Substitute(Escr, "Cento", "Cento e ")
        End If
    End If
    If Num Mod 100 <> 0 Then
        If InStr(1, Escr, "entos", 1) > 0 Then
            Escr = Application.Substitute(Escr, "entos", "entos e ")
        End If
    End If
    If Num Mod 1000 <> 0 Then
        If (Num - (Num \ 1000) * 1000) <= 100 Then             If InStr(1, Escr, "Mil", 1) > 0 Then
                Escr = Application.Substitute(Escr, "Mil", "Mil e ")
            End If
        End If
    End If
End Function

A função suporta números até 9999. Não é muito, mas já quebra um galho.

Bom proveito!

Fonte: BestExcel

Tomás Vásquez
www.tomasvasquez.com.br

Comentários

comentários

3 comentários em “Excel VBA – Escrevendo números por extenso”

Os comentários estão fechados.