简体   繁体   中英

Excel VBA userform next and previous + first and last entry

How can it be difficult to add buttons showing previous and next entries on the userform? There is a bunch of source on the net. I've tried many of them but no go. I have tried to adapt one of the solutions mentioned in this very site, I failed. :(

In the column A there are item numbers (say 180) It may be according to 180 or to as long as it gets. I want to add the previous and next buttons. Then buttons to show the first and last entry.

My code is:

Private Sub UserForm_Initialize()

Dim k As Long, j As Long
Dim rng As Range
Set rng = Worksheets("BİLGİLER").Range("A180")

k = 0: j = 1

vyakinligi.Value = rng.Offset(k).Value
vadsoyad.Value = rng.Offset(k, j).Value: j = j + 1
vmeslegi.Value = rng.Offset(k, j).Value: j = j + 1
visadresi.Value = rng.Offset(k, j).Value: j = j + 1
vceptel.Value = rng.Offset(k, j).Value: j = j + 1

End Sub

'~~> Next Button
Private Sub CommandButton7_Click()
k = k + 1: j = 1

If k > (Sheets("BİLGİLER").Rows.Count - 4) Then
    MsgBox "Max rows Reached"
    Exit Sub
End If

vyakinligi.Value = rng.Offset(k).Value
vadsoyad.Value = rng.Offset(k, j).Value: j = j + 1
vmeslegi.Value = rng.Offset(k, j).Value: j = j + 1
visadresi.Value = rng.Offset(k, j).Value: j = j + 1
vceptel.Value = rng.Offset(k, j).Value: j = j + 1

End Sub


'~~> Previous Button
Private Sub CommandButton8_Click()
k = k - 1: j = 1

If k < 0 Then
    MsgBox "1st Row Reached"
    Exit Sub
End If

vyakinligi.Value = rng.Offset(k).Value
vadsoyad.Value = rng.Offset(k, j).Value: j = j + 1
vmeslegi.Value = rng.Offset(k, j).Value: j = j + 1
visadresi.Value = rng.Offset(k, j).Value: j = j + 1
vceptel.Value = rng.Offset(k, j).Value: j = j + 1

End Sub

Where did I go wrong? What should I do to add the buttons and show previous, next, first and the last entry on the userform?

I managed to run (to adapt) the code finally. I will put it here in case someone makes use of it.

In general tab:

Dim Data As Variant
Dim LastRow As Long
Dim r As Long

The previous and next buttons:

Private Sub CommandButton7_Click()
RangeRow xlNext
End Sub

Private Sub CommandButton8_Click()
RangeRow xlPrevious
End Sub

To userform's initialize section:

With Sheets("BİLGİLER") 'change the name as you wish
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

Data = .Range("A1:AZ" & LastRow).Value 'data range

End With

r = ActiveCell.Row - 1

RangeRow r

And this Subroutine code:

Sub RangeRow(ByVal Direction As Long)
r = IIf(Direction = xlPrevious, r - 1, r + 1)
If r < 2 Then r = 2
If r > LastRow Then r = LastRow

With Me

.sira.Text = Data(r, 1)
.tckn.Text = Data(r, 2)
.oadsoyad.Text = Data(r, 3)
.cinsiyet.Text = Data(r, 4)
.dyeri.Text = Data(r, 5)
.dyili.Text = Data(r, 6)
.ncsn.Text = Data(r, 7)
.babaadi.Text = Data(r, 8)
.anneadi.Text = Data(r, 9)
.kangrb.Text = Data(r, 10)
.oceptel.Text = Data(r, 11)
.oevadresi.Text = Data(r, 12)

End With
End Sub

This code takes the info from the related row on the table and fills the userform textboxes and comboboxes on the usefrorm.

Now it's time to put buttons for the first and the last entry. Any suggestions?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM