Como reparar este código de ingreso

Private Sub CommandButton1_Click()
'Declaración de variables

Dim Continuar As String
Dim TransRowRng As Range
Dim NewRow As Integer
Dim Limpiar As String

Continuar = MsgBox("Ingresar nuevo Miembro?", vbYesNo + vbExclamation, strTitulo)
If Continuar = vbNo Then Exit Sub
'
Cuenta = Application.WorksheetFunction.CountIf(Range("A:A"), Me.TextBox1)
'
If Cuenta > 0 Then
MsgBox "La Cedula'" & Me.TextBox1 & "' ya se encuentra registrada", vbExclamation, strTitulo
Else
Set TransRowRng = ThisWorkbook.Worksheets("BD").Cells(1, 1).CurrentRegion
NewRow = TransRowRng.Rows.Count + 1
With ThisWorkbook.Worksheets("BD")
.Cells(NewRow, 1).value = Val(Me.TextBox1)
If Me.TextBox1 = "" Then
MsgBox "Debe Ingresar un Nombre", vbExclamation, strTitulo
TextBox2.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 2).value = Me.TextBox2
If Me.TextBox3 = "" Then
MsgBox "Debe Ingresar un Apellido", vbExclamation, strTitulo
TextBox2.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 3).value = Me.TextBox3
If Me.OptionButton1 Or Me.OptionButton2 = "" Then
MsgBox "Debe Ingresar tipo de Sexo", vbExclamation, strTitulo
TextBox3.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 4).value = Me.OptionButton1.Caption Or Me.OptionButton2.Caption
If Me.TextBox4 = "" Then
MsgBox "Debe Ingresar Número de Teléfono Local", vbExclamation, strTitulo
TextBox4.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 5).value = Me.TextBox4
If Me.TextBox5 = "" Then
MsgBox "Debe Ingresar Número de Teléfono Local", vbExclamation, strTitulo
TextBox4.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 6).value = Me.TextBox5
If Me.TextBox5 = "" Then
MsgBox "Debe Ingresar Número de Teléfono Celular", vbExclamation, strTitulo
TextBox5.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 7).value = Me.TextBox6
If Me.TextBox6 = "" Then
MsgBox "Debe Ingresar Correo Eléctronico", vbExclamation, strTitulo
TextBox6.SetFocus
SendKeys "{Home}+{End}"
Else
If Me.ComboBox1 = "" Then
MsgBox "Debe Ingresar Status", vbExclamation, strTitulo
ComboBox1.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 8).value = Me.ComboBox1
If Me.TextBox7 = "" Then
MsgBox "Debe Ingresar un Sector", vbExclamation, strTitulo
TextBox7.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 9).value = Me.TextBox8
If Me.TextBox8 = "" Then
MsgBox "Debe Ingresar la Dirección", vbExclamation, strTitulo
TextBox8.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 10).value = Me.TextBox8
If Me.CheckBox1 = "" Then
Me.TextBox12 = ""
Me.TextBox13 = ""
Me.TextBox15 = ""
Else
.Cells(NewRow, 13).value = Me.CheckBox1
If Me.TextBox9 = "" Then
MsgBox "Debe Ingresar N° de Planillas Entregadas", vbExclamation, strTitulo
TextBox9.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 11).value = Me.TextBox9
If Me.TextBox12 = "" Then
MsgBox "Debe Ingresar Total de Electores Aportados", vbExclamation, strTitulo
TextBox12.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 12).value = Me.TextBox12
If Me.TextBox13 = "" Then
MsgBox "Debe Ingresar la Fecha de Entrega", vbExclamation, strTitulo
TextBox13.SetFocus
SendKeys "{Home}+{End}"
Else
.Cells(NewRow, 14).value = Me.TextBox13
If Me.TextBox15 = "" Then
MsgBox "Debe Ingresar el Total de Electores Verificados", vbExclamation, strTitulo
TextBox15.SetFocus
SendKeys "{Home}+{End}"
.Cells(NewRow, 15).value = Me.TextBox15
End With
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
MsgBox "Ingreso de Datos Exitoso.", vbInformation, strTitulo
'Unload Me
End Sub

2 respuestas

Respuesta
1

¿Y qué quieres reparar?

¿Quieres hacerlo más compacto?

¿O quieres primero poner las validaciones y que no escriba en la hoja hasta que todos los campos esté completados?

Me da error de compilacion edn with sin with

La estructura quedaría así:

Private Sub CommandButton1_Click()
'Declaración de variables
Dim Continuar As String
Dim TransRowRng As Range
Dim NewRow As Integer
Dim Limpiar As String
With ThisWorkbook.Worksheets("BD")
    Continuar = MsgBox("Ingresar nuevo Miembro?", vbYesNo + vbExclamation, strTitulo)
    If Continuar = vbNo Then Exit Sub
    '
    Cuenta = Application.WorksheetFunction.CountIf(Range("A:A"), Me.TextBox1)
    If Cuenta > 0 Then
        MsgBox "La Cedula'" & Me.TextBox1 & "' ya se encuentra registrada", vbExclamation, strTitulo
        Exit Sub
    End If
    '
    Set TransRowRng = .Cells(1, 1).CurrentRegion
    NewRow = TransRowRng.Rows.Count + 1
    .Cells(NewRow, 1).Value = Val(Me.TextBox1)
    If Me.TextBox1 = "" Then
        MsgBox "Debe Ingresar un Nombre", vbExclamation, strTitulo
        TextBox2.SetFocus
        SendKeys "{Home}+{End}"
    Else
        .Cells(NewRow, 2).Value = Me.TextBox2
        If Me.TextBox3 = "" Then
            MsgBox "Debe Ingresar un Apellido", vbExclamation, strTitulo
            TextBox2.SetFocus
            SendKeys "{Home}+{End}"
        Else
            .Cells(NewRow, 3).Value = Me.TextBox3
            If Me.OptionButton1 Or Me.OptionButton2 = "" Then
                MsgBox "Debe Ingresar tipo de Sexo", vbExclamation, strTitulo
                TextBox3.SetFocus
                SendKeys "{Home}+{End}"
            Else
                .Cells(NewRow, 4).Value = Me.OptionButton1.Caption Or Me.OptionButton2.Caption
                If Me.TextBox4 = "" Then
                    MsgBox "Debe Ingresar Número de Teléfono Local", vbExclamation, strTitulo
                    TextBox4.SetFocus
                    SendKeys "{Home}+{End}"
                Else
                    .Cells(NewRow, 5).Value = Me.TextBox4
                    If Me.TextBox5 = "" Then
                        MsgBox "Debe Ingresar Número de Teléfono Local", vbExclamation, strTitulo
                        TextBox4.SetFocus
                        SendKeys "{Home}+{End}"
                    Else
                        .Cells(NewRow, 6).Value = Me.TextBox5
                        If Me.TextBox5 = "" Then
                            MsgBox "Debe Ingresar Número de Teléfono Celular", vbExclamation, strTitulo
                            TextBox5.SetFocus
                            SendKeys "{Home}+{End}"
                        Else
                            .Cells(NewRow, 7).Value = Me.TextBox6
                            If Me.TextBox6 = "" Then
                                MsgBox "Debe Ingresar Correo Eléctronico", vbExclamation, strTitulo
                                TextBox6.SetFocus
                                SendKeys "{Home}+{End}"
                            Else
                                If Me.ComboBox1 = "" Then
                                    MsgBox "Debe Ingresar Status", vbExclamation, strTitulo
                                    ComboBox1.SetFocus
                                    SendKeys "{Home}+{End}"
                                Else
                                    .Cells(NewRow, 8).Value = Me.ComboBox1
                                    If Me.TextBox7 = "" Then
                                        MsgBox "Debe Ingresar un Sector", vbExclamation, strTitulo
                                        TextBox7.SetFocus
                                        SendKeys "{Home}+{End}"
                                    Else
                                        .Cells(NewRow, 9).Value = Me.TextBox8
                                        If Me.TextBox8 = "" Then
                                            MsgBox "Debe Ingresar la Dirección", vbExclamation, strTitulo
                                            TextBox8.SetFocus
                                            SendKeys "{Home}+{End}"
                                        Else
                                            .Cells(NewRow, 10).Value = Me.TextBox8
                                            If Me.CheckBox1 = "" Then
                                                Me.TextBox12 = ""
                                                Me.TextBox13 = ""
                                                Me.TextBox15 = ""
                                            Else
                                                .Cells(NewRow, 13).Value = Me.CheckBox1
                                                If Me.TextBox9 = "" Then
                                                    MsgBox "Debe Ingresar N° de Planillas Entregadas", vbExclamation, strTitulo
                                                    TextBox9.SetFocus
                                                    SendKeys "{Home}+{End}"
                                                Else
                                                    .Cells(NewRow, 11).Value = Me.TextBox9
                                                    If Me.TextBox12 = "" Then
                                                        MsgBox "Debe Ingresar Total de Electores Aportados", vbExclamation, strTitulo
                                                        TextBox12.SetFocus
                                                        SendKeys "{Home}+{End}"
                                                    Else
                                                        .Cells(NewRow, 12).Value = Me.TextBox12
                                                        If Me.TextBox13 = "" Then
                                                            MsgBox "Debe Ingresar la Fecha de Entrega", vbExclamation, strTitulo
                                                            TextBox13.SetFocus
                                                            SendKeys "{Home}+{End}"
                                                        Else
                                                            .Cells(NewRow, 14).Value = Me.TextBox13
                                                            If Me.TextBox15 = "" Then
                                                                MsgBox "Debe Ingresar el Total de Electores Verificados", vbExclamation, strTitulo
                                                                TextBox15.SetFocus
                                                                SendKeys "{Home}+{End}"
                                                                .Cells(NewRow, 15).Value = Me.TextBox15
                                                            End If
                                                        End If
                                                    End If
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End If
End With
MsgBox "Ingreso de Datos Exitoso.", vbInformation, strTitulo
'Unload Me
End Sub

Pero de esa forma es muy complejo seguir el código.

Además si te falta algún dato, pero capturaste bien el primer textbox1, el código te almacena el número y como te falta algún dato, el código termina.

Te recomiendo que primero valides los datos, si los datos están completos entonces almacenas los datos en la hoja.

Por ejemplo, te pongo 3 datos; y si es lo que necesitas te sigues con los demás datos.

Private Sub CommandButton1_Click()
'Act.Por.Dante Amor
    If MsgBox("Ingresar nuevo Miembro?", vbYesNo + vbExclamation, strTitulo) = vbNo Then Exit Sub
    '
    If Application.WorksheetFunction.CountIf(Range("A:A"), Me.TextBox1) > 0 Then
        MsgBox "La Cedula'" & Me.TextBox1 & "' ya se encuentra registrada", vbExclamation, strTitulo
        Exit Sub
    End If
    '
    If Me.TextBox1 = "" Then
        MsgBox "Debe Ingresar una Cédula", vbExclamation, strTitulo
        TextBox1.SetFocus
        Exit Sub
    End If
    '
    If Me.TextBox2 = "" Then
        MsgBox "Debe Ingresar un Nombre", vbExclamation, strTitulo
        TextBox2.SetFocus
        Exit Sub
    End If
    '
    If Me.TextBox3 = "" Then
        MsgBox "Debe Ingresar un Apellido", vbExclamation, strTitulo
        TextBox3.SetFocus
        Exit Sub
    End If
    'etc
    'etc
    '
    Set h = ThisWorkbook.Worksheets("BD")
    u = h.Range("A" & Rows.Count).End(xlUp).Row + 1
    h.Cells(u, 1).Value = Val(TextBox1.Value)   'cedula
    h.Cells(u, 2).Value = TextBox2.Value        'nombre
    h.Cells(u, 3).Value = TextBox3.Value        'apellido
    'etc
    'etc
    '
    MsgBox "Ingreso de Datos Exitoso.", vbInformation, strTitulo
End Sub

.

'S aludos. Dante Amor. Recuerda valorar la respuesta. G racias

.

Avísame cualquier duda

.

.

'S aludos. Dante Amor. Recuerda valorar la respuesta. G racias

.

Avísame cualquier duda

.

Respuesta

Fíjate en un ejemplo que te puede orientar, no especificas que tiene de erróneo el código

https://youtu.be/gqZzSfikEYY

https://youtu.be/smL4A-aRjCM

Visita http://programarexcel.com y descarga cientos de ejemplos gratis para que puedas armar tu macro

Suscribe a https://www.youtube.com/channel/UCTKYXi9ljxxOAXXKgwWDDpQ y recibe actualizaciones de ejemplos que se vayan presentando en tu mail

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas