Primero, Siguiente, Anterior, Ultimo registro

ColdGeorge

Active Member
Joined
Aug 21, 2012
Messages
412
Office Version
  1. 2016
Platform
  1. Windows
Hola amigos

En una tabla, guardo los registros que capturo con un formulario, tengo cuatro botones para navegar, para el primer registro uso:

Code:
Private Sub btnprimer_Click()
Me.txtnumero.Value = Range("A2").Value
Me.txtalta.Value = Range("A2").Offset(0, 1).Value
Me.txtnombre.Value = Range("A2").Offset(0, 2).Value
Me.txtsueldo.Value = Range("A2").Offset(0, 3).Value
End Sub

Para el ultimo registro, empleo:

Code:
Private Sub btnultimo_Click()
Me.txtnumero.Value = Range("A" & Rows.Count).End(xlUp).Value
Me.txtalta.Value = Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value
Me.txtnombre.Value = Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value
Me.txtsueldo.Value = Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value
End Sub

Necesito ayuda para el Siguiente y Anterior, gracias de antemano, ColdGeorge
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
¡Hola ColdGeorge! Creo que será mejor guardar el registro actual en una variable. Así es como yo lo haría:
Code:
Private Const MinRow As Long = 2
Private CurrentRow As Long
 
Private Function MaxRow() As Long
    With Cells(MinRow, 1).CurrentRegion
        MaxRow = .Rows.Count - .Row + 1
    End With
End Function
 
Private Sub AnteriorBtn_Click()
    CurrentRow = IIf(CurrentRow - 1 < MinRow, MinRow, CurrentRow - 1)
    UpdateTextBoxes
End Sub
 
Private Sub PrimerBtn_Click()
    CurrentRow = MinRow
    UpdateTextBoxes
End Sub
 
Private Sub SiguienteBtn_Click()
    CurrentRow = IIf(CurrentRow + 1 > MaxRow, MaxRow, CurrentRow + 1)
    UpdateTextBoxes
End Sub
 
Private Sub UltimoBtn_Click()
    CurrentRow = MaxRow
    UpdateTextBoxes
End Sub
 
Private Sub UpdateTextBoxes()
    TextBox1 = Sheet1.Cells(CurrentRow, 1).Text
    TextBox2 = Sheet1.Cells(CurrentRow, 2).Text
    TextBox3 = Sheet1.Cells(CurrentRow, 3).Text
    TextBox4 = Sheet1.Cells(CurrentRow, 4).Text
End Sub
 
Private Sub UserForm_Initialize()
    CurrentRow = MinRow
    UpdateTextBoxes
End Sub
Y aquí esta una muestra: https://drive.google.com/file/d/0B9CuYslrmRbBTHVhbXZrVDJ6enM/edit?usp=sharing
 
Upvote 0
Hola AngelJ

Gracias por tu sugerencia, voy a probarla y te comentaré los resultados.

ColdGeorge
 
Upvote 0
Hola AngelJ

Acertada tu sugerencia, ahora todo esta bajo control, como haría para editar y eliminar registros?

ColdGeorge
 
Upvote 0
Para eliminar registros, puede usar algo como esto:
Code:
Sheet1.Rows(CurrentRow).Delete
Editar es lo contrario de lo que usamos para llenar las cajas de texto:
Code:
Sheet1.Cells(CurrentRow, 1) = TextBox1
 
Upvote 0
Gracias AngelJ

Todo funciona como es debido, saludos a tu tierra.

ColdGeorge
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,568
Members
452,652
Latest member
eduedu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top