Editar registro no form vba excel
Enviado: Qui Mai 12, 2016 10:50 pm
Prezados boa noite, sou novo no que se refere a VBA, e estou com a seguinte dificuldade,
Criei um formulario, onde preencho os dados e estes sao salvos na planilha, ate ai ok.
o Botao excluir funciona bem, bem como o botao pesquisar, so que, quando pesquiso um registro, e altero o mesmo, ao clicar no botao editar, para gravar a alteracao, o registro alterado acaba sendo gravado na linha 1 da planilha,
abaixo segue o codigo que usei, se alguem puder me ajudar agradeço.
Public Function NEWEDITAR() As Integer
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("AGENDA") 'Cadastra ("range dinamica nomeada - Expande com a digitação")
Linha = txtCodigo + 1
'Desprotege a planilha utilizando o password "Senha"
Sheets("AGENDA").Unprotect "1"
With ws
.Cells(Linha, 2) = CMBMESREF
.Cells(Linha, 3) = CMBCELULAEMISSAO
.Cells(Linha, 4) = TXTCLIENTE
.Cells(Linha, 5) = TXTPRODUTO
.Cells(Linha, 6) = TXTAPOLICE
.Cells(Linha, 7) = CMBSISTEMADEPONTA
.Cells(Linha, 8) = CMBAREADETRATAMENTO
.Cells(Linha, 9) = TXTDETALHEDAOCORRENCIA
.Cells(Linha, 10) = OptionButtonSIM
.Cells(Linha, 11) = CMBUSUARIO
.Cells(Linha, 12) = TXTDATAABERTURA
.Cells(Linha, 13) = CMBSTATUSATUAL
.Cells(Linha, 14) = TXTDATACONCLUSAO
.Cells(Linha, 15) = TXTDATACOBRANCA
.Cells(Linha, 16) = TXTDATASOLUCAO
.Cells(Linha, 17) = TXTTEMPODECORRIDO
.Cells(Linha, 18) = TXTOBS1
.Cells(Linha, 19) = TXTOBS2
.Cells(Linha, 20) = TXTOBS3
.Cells(Linha, 21) = TXTOBS4
.Cells(Linha, 22) = txtsolucaoempregada
.Cells(Linha, 23) = OptionButtonsemsolucao
.Cells(Linha, 24) = TXTOCORRENCIA
.Cells(Linha, 1) = cmbNUMCHAMADO.Text 'se mudar essa linha de código par ínicio só vai afetar a col(A)
End With
cmbNUMCHAMADO.Text = "AGENDA" 'Cadastra ("range dinamica nomeada - expande com a digitação")
cmbNUMCHAMADO.Text = txtCodigo - 1
MsgBox ("Dados Atualizados com Sucesso!"), vbInformation, "https://www.google.com.br/?gws_rd=ssl#q=rowsource+vba"
ActiveWorkbook.Save
MsgBox ("Registro Salvo com Sucesso!.")
LIMPAR
CMDGRAVAR.Enabled = True
CMDEDITAR.Enabled = False
End Function
Criei um formulario, onde preencho os dados e estes sao salvos na planilha, ate ai ok.
o Botao excluir funciona bem, bem como o botao pesquisar, so que, quando pesquiso um registro, e altero o mesmo, ao clicar no botao editar, para gravar a alteracao, o registro alterado acaba sendo gravado na linha 1 da planilha,
abaixo segue o codigo que usei, se alguem puder me ajudar agradeço.
Public Function NEWEDITAR() As Integer
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("AGENDA") 'Cadastra ("range dinamica nomeada - Expande com a digitação")
Linha = txtCodigo + 1
'Desprotege a planilha utilizando o password "Senha"
Sheets("AGENDA").Unprotect "1"
With ws
.Cells(Linha, 2) = CMBMESREF
.Cells(Linha, 3) = CMBCELULAEMISSAO
.Cells(Linha, 4) = TXTCLIENTE
.Cells(Linha, 5) = TXTPRODUTO
.Cells(Linha, 6) = TXTAPOLICE
.Cells(Linha, 7) = CMBSISTEMADEPONTA
.Cells(Linha, 8) = CMBAREADETRATAMENTO
.Cells(Linha, 9) = TXTDETALHEDAOCORRENCIA
.Cells(Linha, 10) = OptionButtonSIM
.Cells(Linha, 11) = CMBUSUARIO
.Cells(Linha, 12) = TXTDATAABERTURA
.Cells(Linha, 13) = CMBSTATUSATUAL
.Cells(Linha, 14) = TXTDATACONCLUSAO
.Cells(Linha, 15) = TXTDATACOBRANCA
.Cells(Linha, 16) = TXTDATASOLUCAO
.Cells(Linha, 17) = TXTTEMPODECORRIDO
.Cells(Linha, 18) = TXTOBS1
.Cells(Linha, 19) = TXTOBS2
.Cells(Linha, 20) = TXTOBS3
.Cells(Linha, 21) = TXTOBS4
.Cells(Linha, 22) = txtsolucaoempregada
.Cells(Linha, 23) = OptionButtonsemsolucao
.Cells(Linha, 24) = TXTOCORRENCIA
.Cells(Linha, 1) = cmbNUMCHAMADO.Text 'se mudar essa linha de código par ínicio só vai afetar a col(A)
End With
cmbNUMCHAMADO.Text = "AGENDA" 'Cadastra ("range dinamica nomeada - expande com a digitação")
cmbNUMCHAMADO.Text = txtCodigo - 1
MsgBox ("Dados Atualizados com Sucesso!"), vbInformation, "https://www.google.com.br/?gws_rd=ssl#q=rowsource+vba"
ActiveWorkbook.Save
MsgBox ("Registro Salvo com Sucesso!.")
LIMPAR
CMDGRAVAR.Enabled = True
CMDEDITAR.Enabled = False
End Function