Excel VBA用户表单下一个和上一个+第一个和最后一个条目

时间:2016-08-18 13:49:25

标签: excel button userform

如何在用户表单上添加显示上一个和下一个条目的按钮是多么困难?网上有很多来源。我已经尝试了很多,但没有去。我试图改编这个网站中提到的解决方案之一,但我失败了。 :(

在A栏中有项目编号(比如180)可以根据180或者只要它得到。我想添加上一个和下一个按钮。然后按钮显示第一个和最后一个条目。

我的代码是:

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

我哪里出错了?如何添加按钮并显示用户表单上的上一个,下一个,第一个和最后一个条目?

1 个答案:

答案 0 :(得分:0)

我设法最终运行(以适应)代码。我会把它放在这里以防万一有人利用它。

一般标签:

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

上一个和下一个按钮:

Private Sub CommandButton7_Click()
RangeRow xlNext
End Sub

Private Sub CommandButton8_Click()
RangeRow xlPrevious
End Sub

使用userform的初始化部分:

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

这个子程序代码:

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

此代码从表中的相关行获取信息,并在usefrorm上填充userform文本框和组合框。

现在是时候为第一个和最后一个条目设置按钮了。有什么建议吗?