Forum Discussion
GranSupt
Feb 04, 2026Copper Contributor
VBA Code "Next" button. Should be so simple...
I have the following codes in the click event for a "next" and "prev" buttons on my userform. The prev button works fine, the next button will not advance to the next record in my table, and instead ...
NikolinoDE
Feb 05, 2026Platinum Contributor
Declare your variables at the top of the UserForm
Dim CurrentRow As Long ' Keeps track of which record you are on
Dim LastRow As Long ' Stores the last row of your dataInitialize the UserForm
Private Sub UserForm_Initialize()
' Find the last row of data in column A
LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
' Start with the first record (row 5)
CurrentRow = 5
' Load the first record into the form
LoadRecord CurrentRow
End SubNext Button (CommandButton6)
Private Sub CommandButton6_Click()
' Debug: See current and last row in Immediate Window
Debug.Print "CurrentRow=" & CurrentRow & " LastRow=" & LastRow
' Check if we are already at the last record
If CurrentRow < LastRow Then
' Move to the next row
CurrentRow = CurrentRow + 1
' Load the new record into the form
LoadRecord CurrentRow
Else
' If we are at the last record, show a message
MsgBox "This is the last record."
End If
End SubPrev Button (CommandButton7)
Private Sub CommandButton7_Click()
' Debug: See current and first row
Debug.Print "CurrentRow=" & CurrentRow
' Check if we are already at the first record
If CurrentRow > 5 Then
' Move to the previous row
CurrentRow = CurrentRow - 1
' Load the new record into the form
LoadRecord CurrentRow
Else
' If we are at the first record, show a message
MsgBox "This is the first record."
End If
End SubLoadRecord Subroutine
You probably already have this, but just for clarity...
Sub LoadRecord(RowNumber As Long)
' Example: fill two textboxes from columns A and B
TextBox1.Value = Sheet1.Cells(RowNumber, "A").Value
TextBox2.Value = Sheet1.Cells(RowNumber, "B").Value
End SubUserForm opens → UserForm_Initialize sets CurrentRow = 5 and LastRow dynamically.
Clicking Next → increases CurrentRow until LastRow.
Clicking Prev → decreases CurrentRow until 5.
Messages appear if you try to go past first or last record.
My answers are voluntary and without guarantee!
Hope this will help you.