Вы находитесь на странице: 1из 1

How to create user-forms with navigation buttons coded in VBA to loop through Excel

data. Earlier we had demonstrated how create and manage databases in Excel with
user forms. But many people were not able to implement the concept. Most notably
many found the concept of global variables difficult to use and many just forgot to
declare the global variable at the top of the VBA code. Once you understand the
global variable and how to use the correct sequence in VBA coding of the �back� and
�next� navigation buttons, you�ll be able to create other buttons like �update� and
�clear data�. This will make the managing of databases in Excel with user-forms
easier.

Here�s the complete VBA code to create user-forms with navigation buttons:

Dim currentrow As Long � declare the global variable right at the top

Private Sub UserForm_Initialize()


currentrow = 2
TextBox1 = Cells(currentrow, 1)
TextBox2 = Cells(currentrow, 2)
TextBox3 = Cells(currentrow, 3)

End Sub

Private Sub CommandButton2_Click()


Dim lastrow As Long
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

If currentrow = lastrow Then


MsgBox �you are in the last row! No more data.�
Exit Sub
End If

currentrow = currentrow + 1

TextBox1 = Cells(currentrow, 1)
TextBox2 = Cells(currentrow, 2)
TextBox3 = Cells(currentrow, 3)
End Sub

Private Sub CommandButton3_Click()


If currentrow = 2 Then
MsgBox �You are in the first row!�
Exit Sub
End If

currentrow = currentrow � 1

TextBox1 = Cells(currentrow, 1)
TextBox2 = Cells(currentrow, 2)
TextBox3 = Cells(currentrow, 3)
End Sub

Вам также может понравиться