Private Sub bt_fechar_Click() Unload UserForm1 End Sub Private Sub btn_atualiza_Click() Dim plan As Worksheet Dim codigo As Integer Dim produto As String Dim qtd As Integer Dim venda As Long Dim custo As Long Dim total As Long Dim vencimento As Date Dim dias As Date Dim situacao As Double 'seta a planilha para que não precise se repetir no restante do código Set plan = Sheets("estoque") linha = 2 codigo = txt_codigo outros = txt_produto data = txt_qtd total = txt_total plan.Select linha = plan.Range("A:A").Find(codigo).Row 'Retorna na variável a posição da linha do código With plan .Cells(linha, 1) = txt_codigo.Value .Cells(linha, 2) = txt_produto .Cells(linha, 3) = txt_qtd .Cells(linha, 4) = txt_venda .Cells(linha, 5) = txt_custo .Cells(linha, 6) = txt_total .Cells(linha, 7) = txt_vencimento End With MsgBox ("Dados alterandos com sucesso!!!") 'limpa todos os campos do formulário txt_codigo = "" txt_produto = "" txt_qtd = "" txt_venda = "" txt_custo = "" txt_total = "" 'chama o initialize para recarregar as informções atualizadas na StatusBar Call UserForm1_Initialize End Sub Private Sub btn_cadastrar_Click() Dim plan As Worksheet Dim codigo, linha As Integer Dim produto As Integer Dim qtd As Long Dim total As Long 'seta a planilha para que não precise se repetir no restante do código Set plan = Sheets("estoque") produto = txt_produto = "" qtd = txt_qtd total = txt_total plan.Select 'seleciona a plan Range("A1048576").Select 'seleciona a ultima linha da plan Selection.End(xlUp).Select 'sobe até o primeira célula com conteúdo ActiveCell.Offset(1, 0).Select 'desce para célula em branco linha = ActiveCell.Row 'captura na variável o valor da linha atual With plan .Cells(linha, 1) = txt_codigo.Value .Cells(linha, 2) = txt_produto .Cells(linha, 3) = txt_qtd .Cells(linha, 4) = venda .Cells(linha, 5) = custo .Cells(linha, 6) = total .Cells(linha, 7) = vencimento End With MsgBox ("Dados Cadastrados com sucesso!!!") ActiveWorkbook.Save Unload Me UserForm1.Show End Sub Private Sub CommandButton1_Click() UserForm4.Show End Sub Private Sub Label1_Click() End Sub Private Sub Label2_Click() End Sub Private Sub Label8_Click() End Sub Private Sub txt_codigo_AfterUpdate() 'código sai da macro caso seja atualizado o campo vazio If Me.txt_codigo = "" Then Exit Sub End If btn_atualiza.Enabled = True btn_cadastrar.Enabled = False Dim linha As Integer Dim codigo As Integer codigo = txt_codigo Sheets("estoque").Select On Error GoTo nao_encontrado 'Localiza um registro sem o looping pelo método find linha = Sheets("estoque").Range("A:A").Find(codigo).Row txt_produto = Sheets("estoque").Cells(linha, 2) txt_qtd = Sheets("estoque").Cells(linha, 3) txt_venda = Sheets("estoque").Cells(linha, 4) txt_custo = Sheets("estoque").Cells(linha, 5) txt_total = Sheets("estoque").Cells(linha, 6) txt_vencimento = Sheets("estoque").Cells(linha, 7) txt_dias = Sheets("estoque").Cells(linha, 8) txt_situacao = Sheets("estoque").Cells(linha, 9) Exit Sub nao_encontrado: Dim resposta As String 'cria a variável resposta resposta = MsgBox("Código não encontrado. Deseja cadastrar?", vbYesNo) 'cria a mensagem para determinar qual ação será executada If resposta = vbYes Then ' se a resposta for sim então txt_produto = "" txt_qtd = "" txt_venda = "" txt_custo = "" txt_total = "" txt_vencimeto = "" btn_atualiza.Enabled = False Me.btn_cadastrar.Enabled = True Else txt_produto = "" txt_qtd = "" txt_venda = "" txt_custo = "" txt_total = "" txt_vencimeto = "" btn_atualiza.Enabled = False Me.btn_cadastrar.Enabled = True End If End Sub Private Sub txt_dias_Change() txt_dias.Text = Format(txt_dias.Text, "") End Sub Private Sub txt_produto_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If InStr(1, Chr(32) & Chr(8) & "AÃÁBCÇDEÉÊFGHIÍJKLMNOPQRSTUÚVWXYZaãábcçdeéêfghiíjklmnopqrstuúvwxyz,.", Chr(KeyAscii)) = 0 Then KeyAscii = 0 'Chr(32) = espaço 'Chr(8) = BackSpace End Sub Private Sub txt_qtd_Chager() txt_total.Text = (CDbl(txt_venda.Text) * CDbl(txt_qtd.Text)) End Sub Private Sub txt_qtd_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) txt_total.Text = (CDbl(txt_venda.Text) * CDbl(txt_qtd.Text)) txt_codigo.MaxLength = 8 Select Case KeyAscii Case 8, 48 To 57 ' BackSpace e numericos If Len(txt_qtd) = 3 Or Len(txt_qtd) = 4 Then txt_qtd.Text = txt_qtd.Text & "" SendKeys "{End}", False End If Case Else ' o resto é travado KeyAscii = 0 End Select End Sub Private Sub txt_situacao_Change() txt_situacao.Text = Format(txt_situacao.Text, "") End Sub Private Sub txt_total_Enter() txt_total = Format(txt_total, "Currency") txt_total.Text = (CDbl(txt_venda.Text) * CDbl(txt_qtd.Text)) End Sub Private Sub txt_vencimento_Change() If Len(txt_vencimento) = 2 Or Len(txt_vencimento) = 5 Then txt_vencimento.Text = txt_vencimento.Text & "/" SendKeys "{End}", True End If End Sub Private Sub txt_custo_Enter() txt_custo = Format(txt_custo, "00.00#,##") End Sub Private Sub UserForm1_Initialize() Dim codigo As Integer Dim produto As String Dim qtd As Integer Dim venda As Long Dim custo As Long Dim total As Long Dim linha As Integer Dim acumulador As Currency '*** coloca no StatusBar a quantidade de Itens em estoque Sheets("estoque").Select 'seleciona a plan Range("A1048576").Select 'seleciona a ultima linha da plan Selection.End(xlUp).Select 'sobe até o primeira célula com conteúdo produto = ActiveCell.Row 'captura na variável o valor da linha atual linha = 2 acumulador = 0 '*** Soma o valor total do estoque e coloca no StatusBar Do Until Sheets("estoque").Cells(linha, 1) = "" acumulador = acumulador + Sheets("estoque").Cells(linha, 10) linha = linha + 1 Loop End Sub Private Sub txt_venda_Enter() txt_venda = Format(txt_venda, "00.00#,##") txt_total.Text = (CDbl(txt_venda.Text) * CDbl(txt_qtd.Text)) End Sub Private Sub UserForm_Click() End Sub