jrsilverio
10-25-2022, 05:46 PM
When I click save it doesn't create a new line, it just saves over the top.
Private Sub CommandButton4_Click()
Dim ws As Worksheet
Dim LastRow As Long, x As Long
Set ws = ThisWorkbook.Sheets("Planilha0")
Dim erro As String
erro = ""
'ComboBox2
If Len(ComboBox2) = 0 Then
erro = "Campo de ID inválido" & vbNewLine
End If
'ComboBox3
If Not IsDate(ComboBox3) Then
erro = erro & "Campo de Data inválido" & vbNewLine
End If
'ComboBox4
If Len(ComboBox4) = 0 Then
erro = erro & "Campo da Natureza de Nota Fiscal inválido" & vbNewLine
End If
'ComboBox5
If Len(ComboBox5) = 0 Then
erro = erro & "Campo da Nota Fiscal inválido" & vbNewLine
End If
'ComboBox6
If Len(ComboBox6) = 0 Then
erro = erro & "Campo de Relação de Remessa inválido" & vbNewLine
End If
'ComboBox7
If Len(ComboBox7) = 0 Then
erro = erro & "Campo de Unidade Requisitante inválido!!!"
End If
'ComboBox8
If Len(ComboBox8) = 0 Then
erro = erro & "Campo de Comprador inválido!!!"
End If
'ComboBox8
If Len(ComboBox10) = 0 Then
erro = erro & "Campo de Recebedor inválido!!!"
End If
'ComboBox8
If Len(ComboBox11) = 0 Then
erro = erro & "Campo de Recebimento inválido!!!"
End If
'Error?
If erro <> "" Then
MsgBox erro, vbCritical, "Campo(s) com Erros"
ComboBox4.SetFocus
Exit Sub
End If
With ws
x = 8
Do While .Cells(x, 1) <> ""
x = x + 1
Loop
LastRow = x '.Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(LastRow, 2).Value2 = ComboBox2.Value
.Cells(LastRow, 3).Value2 = CDate(ComboBox3.Value)
.Cells(LastRow, 4).Value2 = ComboBox4.Value
.Cells(LastRow, 5).Value2 = ComboBox5.Value
.Cells(LastRow, 6).Value2 = ComboBox6.Value
.Cells(LastRow, 7).Value2 = ComboBox7.Value
.Cells(LastRow, 8).Value2 = ComboBox8.Value
.Cells(LastRow, 9).Value2 = ComboBox10.Value
.Cells(LastRow, 10).Value2 = ComboBox11.Value
.Cells(LastRow, 11).Value2 = OptionButton2.Value
'---UseForm3---'
'.Cells(LastRow, 12).Value2 = CheckBox1.Value
'.Cells(LastRow, 13).Value2 = CheckBox2.Value
'.Cells(LastRow, 14).Value2 = CheckBox3.Value
'.Cells(LastRow, 15).Value2 = CheckBox4.Value
'.Cells(LastRow, 16).Value2 = CheckBox5.Value
'.Cells(LastRow, 17).Value2 = CheckBox6.Value
'--------------'
.Cells(LastRow, 22).Value2 = TextBox1.Value
End With
ComboBox4.SetFocus
Call MsgBox("Parabéns!" + vbCrLf + "Cadastro realizado com sucesso." + vbCrLf + vbCrLf + _
"IDº : " & ComboBox2, vbExclamation, "Aviso")
End Sub
Private Sub CommandButton4_Click()
Dim ws As Worksheet
Dim LastRow As Long, x As Long
Set ws = ThisWorkbook.Sheets("Planilha0")
Dim erro As String
erro = ""
'ComboBox2
If Len(ComboBox2) = 0 Then
erro = "Campo de ID inválido" & vbNewLine
End If
'ComboBox3
If Not IsDate(ComboBox3) Then
erro = erro & "Campo de Data inválido" & vbNewLine
End If
'ComboBox4
If Len(ComboBox4) = 0 Then
erro = erro & "Campo da Natureza de Nota Fiscal inválido" & vbNewLine
End If
'ComboBox5
If Len(ComboBox5) = 0 Then
erro = erro & "Campo da Nota Fiscal inválido" & vbNewLine
End If
'ComboBox6
If Len(ComboBox6) = 0 Then
erro = erro & "Campo de Relação de Remessa inválido" & vbNewLine
End If
'ComboBox7
If Len(ComboBox7) = 0 Then
erro = erro & "Campo de Unidade Requisitante inválido!!!"
End If
'ComboBox8
If Len(ComboBox8) = 0 Then
erro = erro & "Campo de Comprador inválido!!!"
End If
'ComboBox8
If Len(ComboBox10) = 0 Then
erro = erro & "Campo de Recebedor inválido!!!"
End If
'ComboBox8
If Len(ComboBox11) = 0 Then
erro = erro & "Campo de Recebimento inválido!!!"
End If
'Error?
If erro <> "" Then
MsgBox erro, vbCritical, "Campo(s) com Erros"
ComboBox4.SetFocus
Exit Sub
End If
With ws
x = 8
Do While .Cells(x, 1) <> ""
x = x + 1
Loop
LastRow = x '.Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(LastRow, 2).Value2 = ComboBox2.Value
.Cells(LastRow, 3).Value2 = CDate(ComboBox3.Value)
.Cells(LastRow, 4).Value2 = ComboBox4.Value
.Cells(LastRow, 5).Value2 = ComboBox5.Value
.Cells(LastRow, 6).Value2 = ComboBox6.Value
.Cells(LastRow, 7).Value2 = ComboBox7.Value
.Cells(LastRow, 8).Value2 = ComboBox8.Value
.Cells(LastRow, 9).Value2 = ComboBox10.Value
.Cells(LastRow, 10).Value2 = ComboBox11.Value
.Cells(LastRow, 11).Value2 = OptionButton2.Value
'---UseForm3---'
'.Cells(LastRow, 12).Value2 = CheckBox1.Value
'.Cells(LastRow, 13).Value2 = CheckBox2.Value
'.Cells(LastRow, 14).Value2 = CheckBox3.Value
'.Cells(LastRow, 15).Value2 = CheckBox4.Value
'.Cells(LastRow, 16).Value2 = CheckBox5.Value
'.Cells(LastRow, 17).Value2 = CheckBox6.Value
'--------------'
.Cells(LastRow, 22).Value2 = TextBox1.Value
End With
ComboBox4.SetFocus
Call MsgBox("Parabéns!" + vbCrLf + "Cadastro realizado com sucesso." + vbCrLf + vbCrLf + _
"IDº : " & ComboBox2, vbExclamation, "Aviso")
End Sub