¿Cómo limitar la entrada de un formulario a solo 10 registros, después de esos 10 ya no admita más?

Espero me puedan ayudar, tengo un formulario en donde selecciono varios datos y después los vació en una hoja nueva con un formato, quisiera saber como puedo solo agregar 10 registros por usuario o mejor dicho por cada vez que abran el archivo.

Este es el botón que agrega los datos en otra hoja. ¿Cómo lo limito a 10 registros?

Te hace una pregunta si desea agregar uno más, vuelve a cargar el formulario, si dice no, manda a la hoja donde están los datos guardados

Private Sub CommandButton2_Click()

'INICIO DE VALIDACIONES

Application.ScreenUpdating = False
ActiveWorkbook.RefreshAll
If ComboBox1 = "" Then
MsgBox "Not Selection"
ComboBox1.SetFocus
Else
If ComboBox2 = "" Then
MsgBox "Not Selection"
ComboBox2.SetFocus
Else
If ComboBox3 = "" Then
MsgBox "Not Selection"
ComboBox3.SetFocus
Else
If ComboBox4 = "" Then
MsgBox "Not Selection"
ComboBox4.SetFocus
Else
If ComboBox9 = "" Then
MsgBox "Not Selection"
ComboBox9.SetFocus
Else
If TextBox1 = "" Then
MsgBox "Not selection quantity"
TextBox1.SetFocus
Else
If TextBox2 = "" Then
MsgBox "Selected Quantity"
TextBox2.SetFocus
Else
If TextBox3 = "" Then
Else
limpiar = MsgBox("You want to process data?", vbYesNo, strTitulo)
If limpiar = vbYes Then
With Me
Sheet2.Activate
Sheet2.Unprotect Password:="123"
Range("a5").Select
'hasta que no encuentre una fila vacía...
Do While Not IsEmpty(ActiveCell)
'que vaya bajando una fila
ActiveCell.Offset(1, 0).Select
Loop
'como ahora ya estamos en la fila vacía,
'solo nos queda escribir los datos:
ActiveCell = Historial
ActiveCell.Offset(0, 1) = Me.ComboBox1.Value
ActiveCell.Offset(0, 2) = Me.ComboBox2.Value
ActiveCell.Offset(0, 3) = Me.ComboBox3.Value
ActiveCell.Offset(0, 4) = Me.ComboBox4.Value
ActiveCell.Offset(0, 5) = Me.ComboBox9.Value
ActiveCell.Offset(0, 7) = Me.TextBox1.Value
ActiveCell.Offset(0, 6) = Me.TextBox2.Value
ActiveCell.Offset(0, 0) = Me.TextBox3.Value
ActiveCell.Offset(0, 8) = Me.Label21
Sheet2.Protect Password:="123"
Me.ComboBox1.Value = ""
Me.ComboBox2.Value = ""
Me.ComboBox3.Value = ""
Me.ComboBox4.Value = ""
Me.ComboBox9.Value = ""
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""

End With
With Me
MsgBox ("Data Processed")
Unload Me
almacenar = MsgBox("Add other?", vbYesNo, strTitulo)
If almacenar = vbYes Then
Load UserForm1
UserForm1.Show
End If
End With
Else
If limpiar = vbNo Then
With Me
CommandButton5 = True
End With
End If
ActiveWorkbook.Save
End If
End If
End If
End If
End If
End If
End If
End If
End If
Exit Sub

Respuesta
2

Le hice varios ajustes a tu macro:

  • Si tienes solamente un userform no es necesario poner Me.
  • Tampoco es necesario que pongas With Me
  • Establecí en el objeto h2 la hoja, de esa forma no es necesario activarla
  • Si vas a presionar el botón para almacenar los cambios, no es necesario preguntar.
  • Tampoco veo necesario preguntar si quieres otro registro, simplemente dejas el form activo y si ya no quiere otro registro, simplemente que lo cierre. De esa forma no tienes que estar cerrando y abriendo el form.

Supongo que solamente puedes agregar registros de las filas 5 a la 15, entonces cuando llegue a la fila 16 te va a enviar el mensaje "Se llegó al limite de 10 registros"

Private Sub CommandButton2_Click()
'Act.Por.Dante Amor
    'INICIO DE VALIDACIONES
    Application.ScreenUpdating = False
    ActiveWorkbook.RefreshAll
    If ComboBox1 = "" Then
        MsgBox "Not Selection"
        ComboBox1.SetFocus
        Exit Sub
    End If
    If ComboBox2 = "" Then
        MsgBox "Not Selection"
        ComboBox2.SetFocus
        Exit Sub
    End If
    If ComboBox3 = "" Then
        MsgBox "Not Selection"
        ComboBox3.SetFocus
        Exit Sub
    End If
    If ComboBox4 = "" Then
        MsgBox "Not Selection"
        ComboBox4.SetFocus
        Exit Sub
    End If
    If ComboBox9 = "" Then
        MsgBox "Not Selection"
        ComboBox9.SetFocus
        Exit Sub
    End If
    If TextBox1 = "" Then
        MsgBox "Not selection quantity"
        TextBox1.SetFocus
    End If
    If TextBox2 = "" Then
        MsgBox "Selected Quantity"
        TextBox2.SetFocus
        Exit Sub
    End If
    If TextBox3 = "" Then
        MsgBox "Selected Quantity"
        TextBox3.SetFocus
        Exit Sub
    End If
    'If MsgBox("You want to process data?", vbYesNo, strTitulo) = vbNo Then
    '    Exit Sub
    'End If
    '
    Set h2 = Sheet2
    h2.Unprotect Password:="123"
    fila = 5
    'hasta que no encuentre una fila vacía...
    Do While Not IsEmpty(h2.Cells(fila, "A"))
        fila = fila + 1
    Loop
    If fila = 16 Then
        MsgBox "Se llegó al limite de 10 registros, no se pueden agregar más registros", vbExclamation
        Exit Sub
    End If
    'como ahora ya estamos en la fila vacía, solo nos queda escribir los datos:
    h2.Cells(fila, "A") = Historial
    h2.Cells(fila, "B") = Me.ComboBox1.Value
    h2.Cells(fila, "C") = Me.ComboBox2.Value
    h2.Cells(fila, "D") = Me.ComboBox3.Value
    h2.Cells(fila, "E") = Me.ComboBox4.Value
    h2.Cells(fila, "F") = Me.ComboBox9.Value
    h2.Cells(fila, "G") = Me.TextBox1.Value
    h2.Cells(fila, "H") = Me.TextBox2.Value
    h2.Cells(fila, "A") = Me.TextBox3.Value
    h2.Cells(fila, "J") = Me.Label21
    h2.Protect Password:="123"
    '
    ComboBox1.Value = ""
    ComboBox2.Value = ""
    ComboBox3.Value = ""
    ComboBox4.Value = ""
    ComboBox9.Value = ""
    TextBox1.Value = ""
    TextBox2.Value = ""
    '
    Application.ScreenUpdating = True
    MsgBox ("Data Processed")
'    almacenar = MsgBox("Add other?", vbYesNo, strTitulo)
'    If almacenar = vbNo Then
'        Unload Me
'    End If
End Sub

.

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

.

Avísame cualquier duda

.

1 respuesta más de otro experto

Respuesta
1

Este es un breve ejemplo de como podría ser, la macro lee el valor en una celda en este caso a1 y valida que se mayor a 10 de cumplirse la condición hasta ahí llega el código y la macro acaba, en caso de ser menor a 10 la macro ejecuta tu código y aumenta un valor en la celda a1, en el modulo initialize puedes limpiar el valor al cargarse el formulario o en el evento close poniendo range("a1")=0

Private Sub CommandButton2_Click()
numero = Range("a1")
If numero > 10 Then End
' inserta tu codigo
'
'
Range("a1") = numero + 1
end sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas