JotaPt Posted December 26, 2015 at 02:48 AM Report Share #591054 Posted December 26, 2015 at 02:48 AM Boas festas para todos: Trago aqui um pedido de ajuda para resolver uma questão que já muito tentei resolvê-la, mas ainda não o consegui. Eu quero fazer um recibo, onde se introduz, numa caixa de texto, um número que representa uma quantidade de um produto comprado (quero que nesta "TextBox" o formato de saída seja moeda número com duas casas decimais), noutra caixa de texto digita-se o preço unitário desse produto (quero que nesta "TextBox" o formato de saída, seja moeda em €), e por fim o valor da multiplicação da quantidade pelo preço seja apresentado numa Label (cujo formato de saída, seja moeda em €). Visitem este link, onde tenho uma imagem do que pretendo e a folha de cálculo Excel com o código que fiz até agora. Antecipadamente agradeço a vossa atenção e desejo-vos um excelente ano de 2016. Obrigado JotaPt Link to comment Share on other sites More sharing options...
vikcch Posted December 26, 2015 at 07:13 PM Report Share #591057 Posted December 26, 2015 at 07:13 PM Boas, Tá um bocado confuso mas parece funcionar bem... depois diz se serviu... Private Sub Form_Load() Me.txtQuantidade.Text = Empty Me.txtPreco.Text = Empty Me.lblCusto.Caption = Empty End Sub Private Function multiplicar() Dim sPreco As String, dPreco As Double sPreco = Replace(txtPreco.Text, "€", "") sPreco = Replace(sPreco, ".", "") dPreco = CDbl(Val(Replace(sPreco, ",", "."))) lblCusto.Caption = Format$(dPreco * Val(txtQuantidade.Text), "€ ###,##0.00") End Function Private Sub txtPreco_Change() Call multiplicar End Sub Private Sub txtPreco_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 48 To 57, 8: KeyAscii = KeyAscii Case 44, 46: If InStr(txtPreco.Text, ",") <> 0 Or InStr(txtPreco.Text, ".") <> 0 Then KeyAscii = 0 Else KeyAscii = 44 End If Case Else: KeyAscii = 0 End Select End Sub Private Sub txtPreco_LostFocus() txtPreco.Text = Format$(txtPreco.Text, "€ ###,##0.00") End Sub Private Sub txtQuantidade_Change() Call multiplicar End Sub Private Sub txtQuantidade_KeyPress(KeyAscii As Integer) If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0 End If End Sub Link to comment Share on other sites More sharing options...
JotaPt Posted December 26, 2015 at 09:46 PM Author Report Share #591058 Posted December 26, 2015 at 09:46 PM Boas, amigo Try-Catch... Desejo-te para ti e para os teus, festas felizes. Já fiz a cópia do teu código e fiz alguns ajustes (principalmente no nome das caixas de texto e do lbael). Quando pressione F5, dá erro de compilação ("Procedure declaration does not match description of event or procedure having same name" e para na linha "Private Sub TextBox2_KeyPress(KeyAscii As Integer", onde: TextBox2 <=> txtPreco Será que podes descarregar o ficheiro que disponibilizo na minha DropBox e assim poderes verificar o funcionamento da minha folha de cálculo? Mais uma vez obrigado e Boas Festas e um feliz ano novo. JotaPt Link to comment Share on other sites More sharing options...
vikcch Posted December 26, 2015 at 10:24 PM Report Share #591061 Posted December 26, 2015 at 10:24 PM Obrigado, mas o meu username não é Try-Catch assim como o teu não é null... Experimenta fazer ao contrario, muda antes o nome das textboxs e a label na aplicação para esses do exemplo.... não tenho aqui vba, fiz em vb6 mesmo, mas penso que deveria funcionar... Link to comment Share on other sites More sharing options...
JotaPt Posted December 27, 2015 at 11:34 AM Author Report Share #591067 Posted December 27, 2015 at 11:34 AM Desculpa-me a "troca2 de username, vikcch... Já dei conta do entrave no teu código, (algumas declarações em VBA são ligeiramente diferentes do vb6) A linha: Private Sub TextBox2_KeyPress(KeyAscii As Integer) tem de ser escrita: Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Mas, mesmo com o teu código, os cálculos não são bem executados. Deixo aqui o meu código inicial: Private Sub TextBox1_Change() If Me.TextBox1 = "" Then Exit Sub End If If TextBox1.Value <> "" And TextBox2.Value <> "" Then Label1 = FormatCurrency(CCur(Val(TextBox1.Text) * Val(TextBox2.Text))) End If End Sub Private Sub TextBox2_Change() If Me.TextBox2 = "" Then Exit Sub End If If TextBox1.Value <> "" And TextBox2.Value <> "" Then Label1 = FormatCurrency(CCur(Val(TextBox1.Text) * Val(TextBox2.Text))) End If End Sub Sub buscar_recibo() If Me.lbl_Recibo = "" Then Exit Sub End If Dim plan As Worksheet Dim linha As Integer Dim recibo As Integer Set plan = Sheets("Dados") recibo = lbl_Recibo plan.Select linha = plan.Range("A:A").Find(recibo).Row TextBox1 = plan.Cells(linha, 2) txtDscr = plan.Cells(linha, 3) TextBox2 = FormatCurrency(plan.Cells(linha, 4)) Label1 = FormatCurrency(plan.Cells(linha, 5)) End Sub Private Sub UserForm_Initialize() Sheets("Dados").Select Dim quantidade As Integer quantidade = (Sheets("Dados").Range("A" & Rows.Count).End(xlUp).Row) - 1 Sheets("Dados").Range("A2").Select Me.lbl_Recibo = ActiveCell Call buscar_recibo End Sub Com este código, os cálculos são bem efetuados, mas as caixas de texto não têm quaisquer formatações e assim que eu as formate para que o preço seja em €, os cálculos passam a ser efetuados erradamente. Agradeço, desde já a tua atenção, obrigado... Aguardo a vossa ajuda e desejo-vos boas saídas e excelentes entradas para 2016. Feliz ano de 2016 JotaPt Link to comment Share on other sites More sharing options...
manuel antonio Posted January 16, 2016 at 01:25 PM Report Share #592069 Posted January 16, 2016 at 01:25 PM Ao escreveres FormatCurrency, no final tens que inserir o formato que pretendes, por exemplo "###,##0.00". Agora na linha: Label1 = FormatCurrency(CCur(Val(TextBox1.Text) * Val(TextBox2.Text))) o que significa CCur? Porque não retiras "Val" e em vez de "TextBox1.Text", não colocas "TextBox1.Value"? Atenção que as variáveis que quiseres exibir em €, devem ser declaradas como double, eventualmente como "Variant". Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now