Notifications
Clear all

Erro na hora de salvar, VBA

2 Posts
2 Usuários
0 Reactions
895 Visualizações
 jrs
(@jrs)
Posts: 0
New Member
Topic starter
 

Olá pessoal, tenho o seguinte código aberto que é responsavel por salvar os dados do form na planilha, porém, tentei fazer com que seja obrigatório o preenchimento dos campos para poder salvar, o problema é que mesmo sem preencher, ainda salva...

Private Sub save_click()
Dim ws As Worksheet
Dim LastRow As Long

Set ws = ThisWorkbook.Sheets("Plan1")
With ws
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(LastRow + 1, 1).Value2 = namebox.Value
.Cells(LastRow + 1, 2).Value2 = cepefi.Value
.Cells(LastRow + 1, 3).Value2 = relation.Value
.Cells(LastRow + 1, 4).Value2 = reception.Value
.Cells(LastRow + 1, 5).Value2 = departament.Value

End With

If namebox.Text = Empty Then
MsgBox "Insira todos os dados necessários para concluir o cadastro."
namebox.SetFocus
End If

If cepefi.Text = Empty Then
MsgBox "Insira todos os dados necessários para concluir o cadastro."
namebox.SetFocus
End If

If relation.Text = Empty Then
MsgBox "Insira todos os dados necessários para concluir o cadastro."
namebox.SetFocus
End If

If reception.Text = Empty Then
MsgBox "Insira todos os dados necessários para concluir o cadastro."
namebox.SetFocus
End If

If departament.Text = Empty Then
MsgBox "Insira todos os dados necessários para concluir o cadastro."
namebox.SetFocus
End If

namebox = ""
cepefi = ""
relation = ""
reception = ""
departament = ""

namebox.SetFocus



Columns("A:F").Select
    ActiveWorkbook.Worksheets("Plan1").ListObjects("Tabela1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Plan1").ListObjects("Tabela1").Sort.SortFields.Add _
        Key:=Range("A2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Plan1").ListObjects("Tabela1").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With



End Sub
 
Postado : 14/12/2017 4:09 am
(@klarc28)
Posts: 0
New Member
 
Private Sub save_click()
Dim ws As Worksheet
Dim LastRow As Long

If namebox.Text = Empty Then
MsgBox "Insira todos os dados necessários para concluir o cadastro."
namebox.SetFocus
exit sub
End If

If cepefi.Text = Empty Then
MsgBox "Insira todos os dados necessários para concluir o cadastro."
namebox.SetFocus

exit sub
End If

If relation.Text = Empty Then
MsgBox "Insira todos os dados necessários para concluir o cadastro."
namebox.SetFocus
End If

If reception.Text = Empty Then
MsgBox "Insira todos os dados necessários para concluir o cadastro."
namebox.SetFocus
exit sub
End If

If departament.Text = Empty Then
MsgBox "Insira todos os dados necessários para concluir o cadastro."
namebox.SetFocus
exit sub
End If

Set ws = ThisWorkbook.Sheets("Plan1")
With ws
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(LastRow + 1, 1).Value2 = namebox.Value
.Cells(LastRow + 1, 2).Value2 = cepefi.Value
.Cells(LastRow + 1, 3).Value2 = relation.Value
.Cells(LastRow + 1, 4).Value2 = reception.Value
.Cells(LastRow + 1, 5).Value2 = departament.Value

End With

Columns("A:F").Select
    ActiveWorkbook.Worksheets("Plan1").ListObjects("Tabela1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Plan1").ListObjects("Tabela1").Sort.SortFields.Add _
        Key:=Range("A2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Plan1").ListObjects("Tabela1").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


namebox = ""
cepefi = ""
relation = ""
reception = ""
departament = ""

namebox.SetFocus
End Sub
 
Postado : 14/12/2017 5:11 am