Você está na página 1de 7

Codigo para pesquisar/ salvar/ cadastrar/ deletar/ limpar

MÓDULO 1
Sub CadastrarOp_Clique()

Dim ws1 As Worksheet


Dim ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("formulario")
Set ws2 = ThisWorkbook.Sheets("Base Dados")
Dim OrdemP As String
OrdemP = ws1.Range("Campo0").Value

ws2.Unprotect "sua_senha_aqui"

'restrição - se o campo N° ordem de Produção estiver vazio


If PlanForm.Range("Campo0") = Empty Then
MsgBox "O Número da Ordem de Produção é obrigatório.", vbExclamation, "Cadastro
de OP"
Exit Sub
End If

'restrição - se Codigo diferente de numero


If Not IsNumeric(ws1.Range("Campo1")) Then
PlanForm.Range("Campo1").Value = Empty
End If

'restrição - se algum campo diferente de vazio


If PlanForm.Range("Campo2").Value <> Empty Or PlanForm.Range("Campo1").Value <>
Empty Or PlanForm.Range("Campo3").Value <> Empty Or
PlanForm.Range("Campo4").Value <> Empty Or PlanForm.Range("Campo5").Value <>
Empty Then
MsgBox "Somente o Número da Ordem de Produção ficará registrado. Para salvar
outras informações clique em 'Atualizar'.", vbInformation, "Cadastro de OP"
End If

If Len(OrdemP) <> 11 Or Not IsNumeric(OrdemP) Then


MsgBox "O Número da Ordem de Produção deve ter 11 caracteres e ser composto
apenas por números.", vbExclamation, "Cadastro de OP"
Exit Sub
End If

Dim ultimosCinco As String


ultimosCinco = Right(OrdemP, 5)

If ultimosCinco <> "01001" Then


Dim resposta As VbMsgBoxResult
resposta = MsgBox("Os últimos 5 dígitos do Número da Ordem de Produção não são
'01001'. Deseja cadastrar assim mesmo?", vbYesNo)
If resposta = vbNo Then Exit Sub
End If

'Verificar se já existe na Base de Dados


Dim celula As Range
Set celula = ws2.Columns("a").Find(What:=OrdemP, LookIn:=xlValues, LookAt:=xlWhole)

If Not celula Is Nothing Then


MsgBox "O Número de Ordem de Produção já existe na Base de Dados. Não é
possível cadastrá-lo novamente.", vbExclamation, "Cadastro de OP"
Exit Sub

End If

' Código para cadastrar o valor na coluna A da planilha


Dim ultimaLinha As Long
ultimaLinha = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row + 1
ws2.Cells(ultimaLinha, "A").Value = OrdemP

MsgBox "Número da Ordem de Produção cadastrada com sucesso!", vbExclamation,


"Cadastro de OP"

ws2.Protect "sua_senha_aqui"

End Sub

Sub Consultar_Clique()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("formulario")
Set ws2 = ThisWorkbook.Sheets("Base Dados")

ws1.Unprotect "sua_senha_aqui"
ws2.Unprotect "sua_senha_aqui"

Dim OrdemP As String


OrdemP = ws1.Range("Campo0").Value

Dim celulaOp As Range


Set celulaOp = ws2.Columns("A").Find(What:=OrdemP, LookIn:=xlValues,
LookAt:=xlWhole)

'restrição - se o campo N° ordem de Produção estiver vazio


If PlanForm.Range("Campo0") = Empty Then
MsgBox "O Número da Ordem de Produção é obrigatório.", vbExclamation, "Consulta
de OP"
Exit Sub
End If

If Not celulaOp Is Nothing Then


Dim linha As Long
linha = celulaOp.Row

' Exibir os valores correspondentes na "Planilha1"


ws1.Range("Campo1").Value = ws2.Cells(linha, "B").Value
ws1.Range("Campo2").Value = ws2.Cells(linha, "C").Value
ws1.Range("Campo3").Value = ws2.Cells(linha, "D").Value
ws1.Range("Campo4").Value = ws2.Cells(linha, "E").Value
ws1.Range("Campo5").Value = ws2.Cells(linha, "F").Value

MsgBox "Dados consultados com sucesso!", vbExclamation, "Consulta de OP"


Else
MsgBox "O Número da Ordem de Produção informado não foi encontrado na Base de
Dados.", vbExclamation, "Consulta de OP"
PlanForm.Range("Campo1").Value = Empty
PlanForm.Range("Campo2").Value = Empty
PlanForm.Range("Campo3").Value = Empty
PlanForm.Range("Campo4").Value = Empty
PlanForm.Range("Campo5").Value = Empty

End If

ws1.Protect "sua_senha_aqui"
ws2.Protect "sua_senha_aqui"

End Sub

Sub LimparForm_Clique()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("formulario")
Set ws2 = ThisWorkbook.Sheets("Base Dados")

ws1.Unprotect "sua_senha_aqui"
ws2.Unprotect "sua_senha_aqui"
PlanForm.Range("Campo0").Value = Empty
PlanForm.Range("Campo1").Value = Empty
PlanForm.Range("Campo2").Value = Empty
PlanForm.Range("Campo3").Value = Empty
PlanForm.Range("Campo4").Value = Empty
PlanForm.Range("Campo5").Value = Empty
ws1.Protect "sua_senha_aqui"
ws2.Protect "sua_senha_aqui"

End Sub

Sub AtualizarDados_Clique()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("formulario")
Set ws2 = ThisWorkbook.Sheets("Base Dados")

ws2.Unprotect "sua_senha_aqui"

Dim OrdemP As String


OrdemP = ws1.Range("Campo0").Value

Dim celulaOp As Range


Set celulaOp = ws2.Columns("A").Find(What:=OrdemP, LookIn:=xlValues,
LookAt:=xlWhole)

If Len(OrdemP) <> 11 Or Not IsNumeric(OrdemP) Then


MsgBox "O Número da Ordem de Produção deve ter 11 caracteres e ser composto
apenas por números.", vbExclamation, "Cadastro de OP"
Exit Sub
End If

'restrição - se o campo N° ordem de Produção estiver vazio


If PlanForm.Range("Campo0") = Empty Then
MsgBox "O Número da Ordem de Produção é obrigatório.", vbExclamation, "Atualizar
OP"
Exit Sub
End If

' restrição - O codigo deve ser numérico


If IsNumeric(ws1.Range("Campo1").Value) Then
Else
MsgBox "O Código deve ser composto apenas por números.", vbExclamation,
"Atualizar OP"
Exit Sub
End If

'restrição - se algum campo estiver vazio


If PlanForm.Range("Campo1").Value = Empty Or PlanForm.Range("Campo3").Value =
Empty Or PlanForm.Range("Campo4").Value = Empty Or
PlanForm.Range("Campo5").Value = Empty Then
MsgBox "O Código, Data de Programação, Hora Inicial e Hora Final são obrigatórios.",
vbInformation, "Atualizar OP"
Exit Sub
End If

'Restrição se é uma data válida


If Not IsDate(PlanForm.Range("Campo3")) Then
MsgBox "Por favor, insira uma Data válida.", vbExclamation, "Atualizar OP"
PlanForm.Range("Campo3").Value = Empty
Exit Sub
End If

'Restrição se é uma hora incial válida


If IsDate(PlanForm.Range("Campo4")) Or Not IsNumeric(PlanForm.Range("Campo4"))
Then
MsgBox "Por favor, insira uma Hora Inicial válida.", vbExclamation, "Atualizar OP"
PlanForm.Range("Campo4").Value = Empty
Exit Sub
End If

'Restrição se é uma hora final válida


If IsDate(PlanForm.Range("Campo5")) Or Not IsNumeric(PlanForm.Range("Campo5"))
Then
MsgBox "Por favor, insira uma Hora Final válida.", vbExclamation, "Atualizar OP"
PlanForm.Range("Campo5").Value = Empty
Exit Sub
End If

'Restrição se hora inicial maior que Hora Final


If PlanForm.Range("Campo4") > PlanForm.Range("Campo5") Then
MsgBox "A Hora Inicial deve ser menor que a Hora Final!", vbCritical, "Atualizar OP"
Exit Sub
End If

If Not celulaOp Is Nothing Then


Dim linha As Long
linha = celulaOp.Row
Dim result As VbMsgBoxResult
' Atualizar os dados na "Base Dados"
ws2.Cells(linha, "B").Value = ws1.Range("Campo1").Value
ws2.Cells(linha, "D").Value = ws1.Range("Campo3").Value
ws2.Cells(linha, "E").Value = ws1.Range("Campo4").Value
ws2.Cells(linha, "F").Value = ws1.Range("Campo5").Value

MsgBox "Dados atualizados com sucesso!"


Else
Dim resposta As VbMsgBoxResult
MsgBox "O Número da Ordem de Produção informado não foi encontrado na Base de
Dados.", vbExclamation, "Atualizar OP"

End If

ws2.Protect "sua_senha_aqui"

End Sub

Sub DeletarDados_Clique()

Sub DeletarDados_Clique()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("formulario")
Set ws2 = ThisWorkbook.Sheets("Base Dados")
Dim OrdemP As String
Dim celulaOp As Range

ws1.Unprotect "sua_senha_aqui"
ws2.Unprotect "sua_senha_aqui"

OrdemP = ws1.Range("Campo0").Value
Set celulaOp = ws2.Columns("A").Find(What:=OrdemP, LookIn:=xlValues,
LookAt:=xlWhole)

'restrição - se o campo N° ordem de Produção estiver vazio


If PlanForm.Range("Campo0") = Empty Then
MsgBox "O Número da Ordem de Produção é obrigatório.", vbExclamation, "Atualizar
OP"
Exit Sub
End If

If Len(OrdemP) <> 11 Or Not IsNumeric(OrdemP) Then


MsgBox "O Número da Ordem de Produção deve ter 11 caracteres e ser composto
apenas por números.", vbExclamation, "Cadastro de OP"
Exit Sub
End If
If OrdemP <> "" Then
Dim resposta As Integer
Dim linha As Variant
linha = Application.Match(ws1.Range("Campo0").Value, ws2.Range("A:A"), 0)

' Exibir os valores correspondentes na "Planilha1"


ws1.Range("Campo1").Value = ws2.Cells(linha, "B").Value
ws1.Range("Campo2").Value = ws2.Cells(linha, "C").Value
ws1.Range("Campo3").Value = ws2.Cells(linha, "D").Value
ws1.Range("Campo4").Value = ws2.Cells(linha, "E").Value
ws1.Range("Campo5").Value = ws2.Cells(linha, "F").Value

resposta = MsgBox("Tem certeza que deseja apagar o código pesquisado do banco de


dados?", vbYesNo, "Deletar OP")

If resposta = vbYes Then


If IsError(linha) Then
MsgBox "Número de Ordem de Produção não encontrado no Banco de Dados.",
vbExclamation, "Deletar OP"
Else
Sheets("Base Dados").Rows(linha).Delete
MsgBox "Número da Ordem de Produção foi deletada com sucesso!",
vbExclamation, "Deletar OP"

End If
End If
End If

'Limpar Campos
PlanForm.Range("Campo0").Value = Empty
PlanForm.Range("Campo1").Value = Empty
PlanForm.Range("Campo2").Value = Empty
PlanForm.Range("Campo3").Value = Empty
PlanForm.Range("Campo4").Value = Empty
PlanForm.Range("Campo5").Value = Empty

ws1.Protect "sua_senha_aqui"
ws2.Protect "sua_senha_aqui"

End Sub

Você também pode gostar