Estou desenvolvendo um sistema em VBA e busquei o código na IA. As outras funções funcionam perfeitamente, porém, as funções de alterar e excluir não retornam os dados a partir do número de ofício.
Vou postar o código abaixo:
// Alterar
Private Sub cmdAlterar_Click()
Dim linha As Long
Dim encontrado As Boolean
encontrado = False
If Not ValidarCampos Then Exit Sub
linha = 2 ' Começar da linha 2
With Sheets("AgendaEventos")
Do While .Cells(linha, 1).Value <> ""
If .Cells(linha, 1).Value = Me.txtNumOficio.Text Then
' Atualizar os dados
.Cells(linha, 2).Value = Me.txtEvento.Text
.Cells(linha, 3).Value = Me.cmbCategoria.Text
.Cells(linha, 4).Value = Me.txtResponsavel.Text
.Cells(linha, 5).Value = Me.txtCpfCnpj.Text
.Cells(linha, 6).Value = Me.txtTelefone.Text
.Cells(linha, 7).Value = Me.txtLocal.Text
.Cells(linha, 8).Value = Me.txtData.Text
.Cells(linha, 9).Value = Me.cmbEstimativa.Text
.Cells(linha, 10).Value = Me.cmbSituacao.Text
encontrado = True
Exit Do
End If
linha = linha + 1
Loop
End With
If encontrado Then
MsgBox "Registro alterado com sucesso!", vbInformation
Call cmdLimpar_Click
Else
MsgBox "Número de Ofício não encontrado para alteração!", vbExclamation
End If
End Sub
//excluir
Private Sub cmdExcluir_Click()
If txtNumOficio.Value = "" Then
MsgBox "Informe o Número do Ofício para excluir.", vbExclamation
Exit Sub
End If
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("AgendaEventos")
Dim linha As Long
linha = ProcurarEvento(txtNumOficio.Value)
If linha = 0 Then
MsgBox "Evento não encontrado!", vbExclamation
Else
ws.Rows(linha).Delete
MsgBox "Evento excluído com sucesso!", vbInformation
Call cmdLimpar_Click
End If
End Sub
//validar campos
Private Function ValidarCampos() As Boolean
ValidarCampos = False
If Me.txtNumOficio.Text = "" Or _
Me.txtEvento.Text = "" Or _
Me.cmbCategoria.Text = "" Or _
Me.txtResponsavel.Text = "" Or _
Me.txtCpfCnpj.Text = "" Or _
Me.txtTelefone.Text = "" Or _
Me.txtLocal.Text = "" Or _
Me.txtData.Text = "" Or _
Me.cmbEstimativa.Text = "" Or _
Me.cmbSituacao.Text = "" Then
MsgBox "Preencha todos os campos obrigatórios!", vbExclamation
Exit Function
End If
ValidarCampos = True
End Function
Queria melhorar esse código trazendo os dados para alteração/exclusão, podem me ajudar?
Pergunta
PaiDeLaura
Bom dia!
Estou desenvolvendo um sistema em VBA e busquei o código na IA. As outras funções funcionam perfeitamente, porém, as funções de alterar e excluir não retornam os dados a partir do número de ofício.
Vou postar o código abaixo:
// Alterar
Private Sub cmdAlterar_Click()
Dim linha As Long
Dim encontrado As Boolean
encontrado = False
If Not ValidarCampos Then Exit Sub
linha = 2 ' Começar da linha 2
With Sheets("AgendaEventos")
Do While .Cells(linha, 1).Value <> ""
If .Cells(linha, 1).Value = Me.txtNumOficio.Text Then
' Atualizar os dados
.Cells(linha, 2).Value = Me.txtEvento.Text
.Cells(linha, 3).Value = Me.cmbCategoria.Text
.Cells(linha, 4).Value = Me.txtResponsavel.Text
.Cells(linha, 5).Value = Me.txtCpfCnpj.Text
.Cells(linha, 6).Value = Me.txtTelefone.Text
.Cells(linha, 7).Value = Me.txtLocal.Text
.Cells(linha, 8).Value = Me.txtData.Text
.Cells(linha, 9).Value = Me.cmbEstimativa.Text
.Cells(linha, 10).Value = Me.cmbSituacao.Text
encontrado = True
Exit Do
End If
linha = linha + 1
Loop
End With
If encontrado Then
MsgBox "Registro alterado com sucesso!", vbInformation
Call cmdLimpar_Click
Else
MsgBox "Número de Ofício não encontrado para alteração!", vbExclamation
End If
End Sub
//excluir
Private Sub cmdExcluir_Click()
If txtNumOficio.Value = "" Then
MsgBox "Informe o Número do Ofício para excluir.", vbExclamation
Exit Sub
End If
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("AgendaEventos")
Dim linha As Long
linha = ProcurarEvento(txtNumOficio.Value)
If linha = 0 Then
MsgBox "Evento não encontrado!", vbExclamation
Else
ws.Rows(linha).Delete
MsgBox "Evento excluído com sucesso!", vbInformation
Call cmdLimpar_Click
End If
End Sub
//validar campos
Private Function ValidarCampos() As Boolean
ValidarCampos = False
If Me.txtNumOficio.Text = "" Or _
Me.txtEvento.Text = "" Or _
Me.cmbCategoria.Text = "" Or _
Me.txtResponsavel.Text = "" Or _
Me.txtCpfCnpj.Text = "" Or _
Me.txtTelefone.Text = "" Or _
Me.txtLocal.Text = "" Or _
Me.txtData.Text = "" Or _
Me.cmbEstimativa.Text = "" Or _
Me.cmbSituacao.Text = "" Then
MsgBox "Preencha todos os campos obrigatórios!", vbExclamation
Exit Function
End If
ValidarCampos = True
End Function
Queria melhorar esse código trazendo os dados para alteração/exclusão, podem me ajudar?
No aguardo e desde já agradeço []'s
Link para o comentário
Compartilhar em outros sites
1 resposta a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.