Página 1 de 1

Formatar textbox pra valores negativos e %

Enviado: 15 Jul 2020 às 15:03
por AlanBC
Ola Master's

Preciso de uma ajuda.
Tenho q formatar textbox pra q assuma valores negativos no form , qd arrega para o form e qd envia pra plan, como numero negativo. .
E tmb outra textbox com percentual tmb negativo(tipo o desconto concedido).
Digitou na textbox qq valor e passa a negativo.
Nao sei se no evento change, afterupdate ou exit...
Pode ajudar com essa macro?
Obrigado
Abraços... Vlw.

Re: Formatar textbox pra valores negativos e %

Enviado: 15 Jul 2020 às 16:46
por babdallas
Ao deixar o textbox (AfterUpdate ou Exit) você quer que formate para %? O usuário vai digitar -20 e deverá aparecer -20%. Se digitar 15, vai aparecer 15%. É isso?

Re: Formatar textbox pra valores negativos e %

Enviado: 15 Jul 2020 às 18:37
por AlanBC
babdallas escreveu:Ao deixar o textbox (AfterUpdate ou Exit) você quer que formate para %? O usuário vai digitar -20 e deverá aparecer -20%. Se digitar 15, vai aparecer 15%. É isso?
amigo obrigado pela atenção...
praticamente isso... mas ao deixar o textbox sempre vai assumir valor negativo: digitar 1500 "aparece" -1.500,00 ; digitar 20 (na textboxdesconto) "aparece" -20,00% ou -0,20
desse forma.

Re: Formatar textbox pra valores negativos e %

Enviado: 15 Jul 2020 às 19:02
por babdallas
Veja se isso ajuda:
Código: Selecionar todos
Private Sub txtValor_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If VBA.IsNumeric(txtValor.Value) Then
        txtValor.Value = VBA.Format(txtValor.Value / 100, "-0,0%")
    End If
End Sub

Re: Formatar textbox pra valores negativos e %

Enviado: 16 Jul 2020 às 21:00
por AlanBC
babdallas escreveu:Veja se isso ajuda:
Código: Selecionar todos
Private Sub txtValor_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If VBA.IsNumeric(txtValor.Value) Then
        txtValor.Value = VBA.Format(txtValor.Value / 100, "-0,0%")
    End If
End Sub
amigo... obrigado.
ajudou sim! mas, caso digite na textbox algum caracter sem ser numerico, poderia apagar esse caracter e fixar o curso na textbox... como ficaria isso? pode concluir por favor?
obrigado. abraços

Re: Formatar textbox pra valores negativos e %

Enviado: 17 Jul 2020 às 07:44
por babdallas
Veja isso:
Código: Selecionar todos
Private Sub txtValor_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim dblValor        As Double

    If VBA.IsNumeric(txtValor.Value) Then
        dblValor = VBA.CDbl(txtValor.Value)
        txtValor.Value = VBA.Format(dblValor / 100, "-#,##0.0%")
    End If
End Sub


Private Sub txtValor_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case 8, 48 To 57
            
        Case 44
            If InStr(txtValor.Text, ",") Then KeyAscii = 0
        Case Else
            KeyAscii = 0
    End Select
End Sub

Re: Formatar textbox pra valores negativos e %

Enviado: 18 Jul 2020 às 15:44
por AlanBC
babdallas escreveu:Veja isso:
Código: Selecionar todos
Private Sub txtValor_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim dblValor        As Double

    If VBA.IsNumeric(txtValor.Value) Then
        dblValor = VBA.CDbl(txtValor.Value)
        txtValor.Value = VBA.Format(dblValor / 100, "-#,##0.0%")
    End If
End Sub


Private Sub txtValor_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case 8, 48 To 57
            
        Case 44
            If InStr(txtValor.Text, ",") Then KeyAscii = 0
        Case Else
            KeyAscii = 0
    End Select
End Sub
Muito obrigado amigo...
era o q precisava. muito bom!!! funcionando perfeitamente...
abraços.