Operações em TextBo...
 
Notifications
Clear all

Operações em TextBox

4 Posts
2 Usuários
0 Reactions
970 Visualizações
(@billo)
Posts: 0
New Member
Topic starter
 

Bom dia senhores, sou iniciante no mundo VBA .
No meu useform eu tenho várias TextBox e gostaria de realizar operações entre elas, vou dar um exemplo de como eu uso uma função no excel e não estou conseguindo aplicar em VBA.
SE(F10>1,05*H4;(F10-H4);0)
Coloquei os valores das células para ficar mais fácil de entender.
Criei este código para o VBA só que não está dando certo.

If TextBox69.Value > (1.05 * TextBox71.Value) Then
TextBox68.Value = (TextBox69.Value - TextBox71.Value)
Else
TextBox68.value = 0
End If

Também gostaria de saber se consigo preencher uma TextBox automaticamente com o resultado dessa operação, sem precisar utilizar um botão.

Não sei se fui claro o suficiente em minhas dúvidas.

Obrigado desde já.

 
Postado : 03/09/2015 8:07 am
(@nelson-s)
Posts: 0
New Member
 

Tente assim:

Private Sub TextBox69_Change()

    Dim sng1 As Single
    Dim sng2 As Single
    
    If Len(Trim(TextBox69)) = 0 Then
        TextBox68.Value = ""
    ElseIf Len(Trim(TextBox69)) > 0 And Len(Trim(TextBox71)) > 0 Then
        sng1 = CSng(TextBox69.Value)
        sng2 = CSng(TextBox71.Value)
        If sng1 > (1.05 * sng2) Then
            TextBox68.Value = sng1 - sng2
        Else
            TextBox68.Value = 0
        End If
    End If
    
End Sub

Private Sub TextBox71_Change()

    Dim sng1 As Single
    Dim sng2 As Single
    
    If Len(Trim(TextBox71)) = 0 Then
        TextBox68.Value = ""
    ElseIf Len(Trim(TextBox69)) > 0 And Len(Trim(TextBox71)) > 0 Then
        sng1 = CSng(TextBox69.Value)
        sng2 = CSng(TextBox71.Value)
        If sng1 > (1.05 * sng2) Then
            TextBox68.Value = sng1 - sng2
        Else
            TextBox68.Value = 0
        End If
    End If

End Sub
 
Postado : 03/09/2015 9:59 am
(@billo)
Posts: 0
New Member
Topic starter
 

Muito Obrigado mesmo Nelson, deu certo aqui. Agora vou tentar compreender o código para aplicar em outros casos.
Abraço

Tente assim:

Private Sub TextBox69_Change()

    Dim sng1 As Single
    Dim sng2 As Single
    
    If Len(Trim(TextBox69)) = 0 Then
        TextBox68.Value = ""
    ElseIf Len(Trim(TextBox69)) > 0 And Len(Trim(TextBox71)) > 0 Then
        sng1 = CSng(TextBox69.Value)
        sng2 = CSng(TextBox71.Value)
        If sng1 > (1.05 * sng2) Then
            TextBox68.Value = sng1 - sng2
        Else
            TextBox68.Value = 0
        End If
    End If
    
End Sub

Private Sub TextBox71_Change()

    Dim sng1 As Single
    Dim sng2 As Single
    
    If Len(Trim(TextBox71)) = 0 Then
        TextBox68.Value = ""
    ElseIf Len(Trim(TextBox69)) > 0 And Len(Trim(TextBox71)) > 0 Then
        sng1 = CSng(TextBox69.Value)
        sng2 = CSng(TextBox71.Value)
        If sng1 > (1.05 * sng2) Then
            TextBox68.Value = sng1 - sng2
        Else
            TextBox68.Value = 0
        End If
    End If

End Sub
 
Postado : 03/09/2015 12:03 pm
(@nelson-s)
Posts: 0
New Member
 

Marque o tópico como [RESOLVIDO] e clique no ícone da "mãozinha" *lado direito do botão "CITAR") se achou que a resposta foi útil.
Abraço.

 
Postado : 03/09/2015 1:00 pm