Botón siguiente en formulario Excel

Estoy creando un formulario en Excel con varios ejemplos que me he encontrado en varias páginas.

La idea es que al momento de hacer doble click en alguna celda de la columna "A", se abra el formulario y se carguen los datos de esa fila en el formulario, posterior a esto, al hacer click en el botón "Siguiente" o "Anterior" se carguen en el formulario los datos respectivos.

El problema que tengo es, por ejemplo, si hago doble clic en el penúltimo registro(se cargan los datos en el formulario) y al hacer click sobre el botón "Siguiente" no me muestra los datos del siguiente, sino que me muestra los datos del segundo registro de la tabla y si continuo haciendo click en el botón "Siguiente" si se cargan los datos en el orden correcto.

Lo mismo pasa si hago doble click en cualquier registro de la tabla, todos se devuelven al segundo registro.

Código de la hoja Excel:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    Set Activador = Range("A2:A10000") ' También puedes utilizar el nombre de un rango que exista en la hoja activa
    If Not Intersect(Target, Activador) Is Nothing Then
  UserForm1.Show
  Unload UserForm1
  UserForm1.Show
    End If
End Sub

Código del formulario:

'variables a emplear entre los diferentes procedimientos más abajo
Dim fila As Long
Dim NumItems As Long
Public Sub cmdCargar_Click()
' al hacer doble click se cargan los datos de la fila en el formulario
Txtid.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 1).Value
TxtNombre.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 2).Value
TxtApellido.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 3).Value
txtEdad.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 4).Value
txtSexo.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 5).Value
End Sub
Private Sub UserForm_Initialize()
Dim filaIni As Long
'damos valores de partida
fila = 2
'rellenamos el formulario
RecuperaDatos (fila)
'y determinamos el número total de registros
NumItems = WorksheetFunction.CountA(Range("A:A")) - 1
Call cmdCargar_Click
End Sub
'Gestionamos los botones  de Siguiente y Anterior
Private Sub cmdSiguiente_Click()
'controlamos el botón de Siguiente
'limitándolo por el último registro
If fila <= NumItems Then
    fila = fila + 1
    RecuperaDatos (fila)
Else
    MsgBox "Último registro"
End If
End Sub
Private Sub cmdAnterior_Click()
'controlamos el botón de Anterior
'limitándolo por el primer registro
If fila > 2 Then
    fila = fila - 1
    RecuperaDatos (fila)
Else
    MsgBox "Primera entrada!!"
End If
End Sub
Private Sub cmdFin_Click()
fila = NumItems + 1
RecuperaDatos (fila)
End Sub
Private Sub cmdInicio_Click()
fila = 2
RecuperaDatos (2)
End Sub
'Procedimiento para recuperar datos de la hoja hacia el UserForm
Private Sub RecuperaDatos(nRow As Long)
    Me.Txtid.Value = Cells(nRow, 1)
    Me.TxtNombre.Value = Cells(nRow, 2)
    Me.TxtApellido.Value = Cells(nRow, 3)
    Me.txtEdad.Value = Cells(nRow, 4)
    Me.txtSexo.Value = Cells(nRow, 5)
End Sub

3 respuestas

Respuesta
4

Desde mi Blog podrás descargar un libro con un formulario que presenta los botones Siguiente y Anterior. Es un ejemplo sencillo pero te servirá para tomar nota de cómo indicar las referencias o ubicación de la celda activa.

Descarga el libro desde aquí.

Este tema se encuentra desarrollado en el video Nº 6 de mi canal.

Respuesta
2
Respuesta
1

Hay que corregir ActiveCell. Row para obtener el número de fila del rango activo en el evento cmdCargar_Click

'variables a emplear entre los diferentes procedimientos más abajo
Dim NumItems As Long
Public Sub cmdCargar_Click()
    ' al hacer doble click se cargan los datos de la fila en el formulario
    Txtid.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 1).Value
    TxtNombre.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 2).Value
    TxtApellido.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 3).Value
    txtEdad.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 4).Value
    txtSexo.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 5).Value
variables a emplear entre los diferentes procedimientos más abajo
Dim NumItems As Long
Public Sub cmdCargar_Click()
    ' al hacer doble click se cargan los datos de la fila en el formulario
    Txtid.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 1).Value
    TxtNombre.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 2).Value
    TxtApellido.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 3).Value
    txtEdad.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 4).Value
    txtSexo.Text = Worksheets("Hoja1").Cells(ActiveCell.Row, 5).Value
End Sub
Private Sub UserForm_Initialize()
    Dim filaIni As Long
    'rellenamos el formulario
    RecuperaDatos (ActiveCell.Row)
    'determinamos el número total de registros
    NumItems = WorksheetFunction.CountA(Worksheets("Hoja1").Range("A:A")) - 1
    Call cmdCargar_Click
End Sub
'Gestionamos los botones de Siguiente y Anterior
Private Sub cmdSiguiente_Click()
    'controlamos el botón de Siguiente limitándolo por el último registro
    If ActiveCell.Row < NumItems + 1 Then
        RecuperaDatos (ActiveCell.Row + 1)
    Else
        MsgBox "Último registro"
    End If
End Sub
Private Sub cmdAnterior_Click()
    'controlamos el botón de Anterior limitándolo por el primer registro
    If ActiveCell.Row > 2 Then
        RecuperaDatos (ActiveCell.Row - 1)
    Else
        MsgBox "Primera entrada!!"
    End If
End Sub
Private Sub cmdFin_Click()
    RecuperaDatos (NumItems + 1)
End Sub
Private Sub cmdInicio_Click()
    RecuperaDatos (2)
End Sub
'Procedimiento para recuperar datos de la hoja hacia el UserForm
Private Sub RecuperaDatos(nRow As Long)
    Me.Txtid.Value = Worksheets("Hoja1").Cells(nRow, 1)
    Me.TxtNombre.Value = Worksheets("Hoja1").Cells(nRow, 2)
    Me.TxtApellido.Value = Worksheets("Hoja1").Cells(nRow, 3)
    Me.txtEdad.Value = Worksheets("Hoja1").Cells(nRow, 4)
    Me.txtSexo.Value = Worksheets("Hoja1").Cells(nRow, 5)
End Sub

Muchas gracias por tu  respuesta 

Solamente me deja pasar un registro y luego no funciona. 

Pasa lo mismo en ambos botones (anterior y siguiente) 

Utilizar ActiveCell. Row en los botones "Siguiente" y "Anterior", debes utilizar la variable fila para mantener

'variables a emplear entre los diferentes procedimientos más abajo
Dim fila As Long
Dim NumItems As Long
Public Sub cmdCargar_Click()
    ' al hacer doble clic se cargan los datos de la fila en el formulario
    Txtid.Text = Worksheets("Hoja1").Cells(fila, 1).Value
    TxtNombre.Text = Worksheets("Hoja1").Cells(fila, 2).Value
    TxtApellido.Text = Worksheets("Hoja1").Cells(fila, 3).Value
    txtEdad.Text = Worksheets("Hoja1").Cells(fila, 4).Value
    txtSexo.Text = Worksheets("Hoja1").Cells(fila, 5).Value
End Sub
Private Sub UserForm_Initialize()
    Dim filaIni As Long
    ' rellenamos el formulario
    fila = 2
    RecuperaDatos fila
    ' determinamos el número total de registros
    NumItems = WorksheetFunction.CountA(Worksheets("Hoja1").Range("A:A")) - 1
    Call cmdCargar_Click
End Sub
' Gestionamos los botones de Siguiente y Anterior
Private Sub cmdSiguiente_Click()
    ' controlamos el botón de Siguiente limitándolo por el último registro
    If fila < NumItems Then
        fila = fila + 1
        RecuperaDatos fila
    Else
        MsgBox "Último registro"
    End If
End Sub
Private Sub cmdAnterior_Click()
    ' controlamos el botón de Anterior limitándolo por el primer registro
    If fila > 2 Then
        fila = fila - 1
        RecuperaDatos fila
    Else
        MsgBox "Primera entrada!!"
    End If
End Sub
Private Sub cmdFin_Click()
    fila = NumItems + 1
    RecuperaDatos fila
End Sub
Private Sub cmdInicio_Click()
    fila = 2
    RecuperaDatos fila
End Sub
' Procedimiento para recuperar datos de la hoja hacia el UserForm
Private Sub RecuperaDatos(nRow As Long)
    Me.Txtid.Value = Worksheets("Hoja1").Cells(nRow, 1)
    Me.TxtNombre.Value = Worksheets("Hoja1").Cells(nRow, 2)
    Me.TxtApellido.Value = Worksheets("Hoja1").Cells(nRow, 3)
    Me.txtEdad.Value = Worksheets("Hoja1").Cells(nRow, 4)
    Me.txtSexo.Value = Worksheets("Hoja1").Cells(nRow, 5)
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas