[英]Excel Userform next button go to next row cell
我在3列中有一組數據:姓名,年齡,工作。
我整理了一個用戶窗體,用戶可以使用它在3個單獨的文本框(TextBox1,TextBox2和TextBox3)中查找姓名,年齡和工作。 它從頂部開始,這是通過設計完成的,效果很好。 我還有一個“下一步”按鈕,當用戶單擊“下一步”時,它將轉到列表中的下一項。
因此,當用戶單擊下一步時,理想情況下應轉到第3行數據(第1行=標題)。
碼:
Private Sub CommandButton3_Click()
Dim AANo As String
Dim AANa As String
Dim AAEm As String
Dim NextLR As Long
NextLR = Sheets("AASD").Cells(Rows.count, "QH").End(xlUp).row
For i = 2 To NextLR
With Worksheets("AASD")
Name = .Range(i, 8).Value
Age = .Range(i, 9).Value
Job = .Range(i, 10).Value
End With
TextBox1.Value = Name
TextBox2.Value = Age
TextBox3.Value = Job
Next i
End Sub
從理論上講這應該起作用,但事實並非如此。 我不確定哪里出了問題。 有什么建議我做錯了嗎?
謝謝
修改后的代碼:
Private Sub CommandButton3_Click()
Dim AANo As String
Dim AANa As String
Dim AAEm As String
Dim NextLR As Long
Dim count As Long
NextLR = Sheets("AASD").Cells(Rows.count, "QH").End(xlUp).row
count = 2
For i = count To NextLR
With Worksheets("AASD")
AANo = .Range(i, 8).Value
AAName = .Range(i, 9).Value
AAEm = .Range(i, 10).Value
End With
TextBox1.Value = AANo
TextBox2.Value = AANa
TextBox3.Value = AAEm
count = count + 1
Exit For
Next i
End Sub
詳細說明我的評論,例如:
Public k as Long
Private Sub CommandButton3_Click()
Dim lr as Long
with sheets("AASD")
lr = .cells(.rows.count,8).end(xlup).row
If k > lr OR k <3 then k = 3
TextBox1.Value = .Cells(k, 8).Value
TextBox2.Value = .Cells(k, 9).Value
TextBox3.Value = .Cells(k, 10).Value
end with
k = k + 1
End Sub
如果沿組合框的路徑查找值,則可以在userform_initialize()期間使用以下方法設置組合框數組:
dim arr() as variant, lr as long
with sheets("AASD")
lr = .cells(.rows.count,8).end(xlup).row
arr = .range(.cells(3,8),.cells(lr,8)).Value
UserForm1.ComboBox1.List = arr
end with
添加另一個文本框並使它不可見,並使用它來計算下一次單擊的次數。
Private Sub CommandButton3_Click()
Dim AANo As String
Dim AANa As String
Dim AAEm As String
Dim NextLR As Long
Dim counter As Long
NextLR = Sheets("AASD").Cells(Rows.count, "QH").End(xlUp).row
counter = me.counterBox + 1 'because on first time it will be empty
With Worksheets("AASD")
AANo = .Range(counter, 8).Value
AAName = .Range(counter, 9).Value
AAEm = .Range(counter, 10).Value
End With
TextBox1.Value = AANo
TextBox2.Value = AANa
TextBox3.Value = AAEm
Me.counterBox = Iif(counter = NextLR, 0, counter)
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.