简体   繁体   English

Excel VBA 表单中的下一个和上一个按钮

[英]Next and Previous Buttons in Excel VBA Form

I have a form I am working on that will collect a set of data.我有一个正在处理的表单,它将收集一组数据。 I have text boxes, combo boxes, and a couple of option buttons.我有文本框、组合框和几个选项按钮。 I have tried multiple ways to create the proper code to go to a previous record and to a next record but I cannot seem to get the code to work.我尝试了多种方法来创建正确的代码以转到上一条记录和下一条记录,但我似乎无法使代码正常工作。 I am new at this but hoping someone can assist.我是新手,但希望有人可以提供帮助。 I can also e-mail the form over if that helps.如果有帮助,我也可以通过电子邮件发送表格。 I tried adding TraverseData to clean up and make it easier to input the data.我尝试添加 TraverseData 来清理并使其更容易输入数据。 I seem to have the next button working but I keep getting the 1004 error with the previous button that cycles back to the TraverseData.我似乎有下一个按钮工作,但我不断收到 1004 错误与循环回到 TraverseData 的前一个按钮。 My code is below and any assistance is greatly appreciated.我的代码如下,非常感谢任何帮助。

Private Sub cmdprevious_Click()
Dim nCurrentRow As Long
Do
nCurrentRow = nCurrentRow - 1
TraverseData (nCurrentRow)
Loop Until nCurrentRow = 1 Or Sheets(1).Cells(nCurrentRow, 1).Value = Me.txtname.Value

If nCurrentRow = 1 Then
MsgBox "This is the last entry.", , "Alert!"

cmdprevious.Enabled = False
  End If
End Sub

Private Sub Cmdnext_Click()
Dim nCurrentRow As Long
Do
    nCurrentRow = nCurrentRow + 1
    TraverseData (nCurrentRow)
Loop Until Sheet1.Cells(nCurrentRow, 1).Value = "" Or Sheet1.Cells(nCurrentRow, 1).Value = 
Me.txtname.Value


End Sub
Private Sub TraverseData(nCurrentRow As Long)

Me.txtname.Value = Sheet1.Cells(nCurrentRow, 1)
Me.txtposition.Value = Sheet1.Cells("nCurrentRow, 2")
Me.txtassigned.Value = Sheet1.Cells(nCurrentRow, 3)
Me.cmbsection.Value = Sheet1.Cells(nCurrentRow, 4)
 Me.txtdate.Value = Sheet1.Cells(nCurrentRow, 5)
 Me.txtjoint.Value = Sheet1.Cells(nCurrentRow, 7)
 Me.txtDAS.Value = Sheet1.Cells(nCurrentRow, 8)
Me.txtDEROS.Value = Sheet1.Cells(nCurrentRow, 9)
Me.txtDOR.Value = Sheet1.Cells(nCurrentRow, 10)
Me.txtTAFMSD.Value = Sheet1.Cells(nCurrentRow, 11)
Me.txtDOS.Value = Sheet1.Cells(nCurrentRow, 12)
Me.txtPAC.Value = Sheet1.Cells(nCurrentRow, 13)
Me.ComboTSC.Value = Sheet1.Cells(nCurrentRow, 14)
Me.txtTSC.Value = Sheet1.Cells(nCurrentRow, 15)
Me.txtAEF.Value = Sheet1.Cells(nCurrentRow, 16)
Me.txtPCC.Value = Sheet1.Cells(nCurrentRow, 17)
Me.txtcourses.Value = Sheet1.Cells(nCurrentRow, 18)
Me.txtseven.Value = Sheet1.Cells(nCurrentRow, 19)
Me.txtcle.Value = Sheet1.Cells(nCurrentRow, 20)

End Sub

Private Sub UserForm_Initialize()
Dim nCurrentRow As Long
Dim currentrow As Long
Dim lastrow As Long

txtname = Cells(nCurrentRow, 1)
txtposition = Cells(nCurrentRow, 2)
txtassigned = Cells(nCurrentRow, 3)
cmbsection = Cells(nCurrentRow, 4)
txtdate = Cells(nCurrentRow, 5)
txtjoint = Cells(nCurrentRow, 7)
txtDAS = Cells(nCurrentRow, 8)
txtDEROS = Cells(nCurrentRow, 9)
txtDOR = Cells(nCurrentRow, 10)
txtTAFMSD = Cells(nCurrentRow, 11)
txtDOS = Cells(nCurrentRow, 12)
txtPAC = Cells(nCurrentRow, 13)
ComboTSC = Cells(nCurrentRow, 14)
txtTSC = Cells(nCurrentRow, 15)
txtAEF = Cells(nCurrentRow, 16)
txtPCC = Cells(nCurrentRow, 17)
txtcourses = Cells(nCurrentRow, 18)
txtseven = Cells(nCurrentRow, 19)
txtcle = Cells(nCurrentRow, 20)

With UserForm1.cmbsection

    .AddItem "Law Office Superintendent"
    .AddItem "NCOIC, Legal Office"
    .AddItem "NCOIC, Training & Readiness"
    .AddItem "NCOIC, Military Justice"
    .AddItem "NCOIC, Adverse Actions"
    .AddItem "NCOIC, General Law"
    .AddItem "NCOIC, International Law"
    .AddItem "NCOIC, Civil Law"
    .AddItem "NCOIC, Other"
    .AddItem "Military Justice Paralegal"
    .AddItem "General Law Paralegal"
    .AddItem "International Law Paralegal"
    .AddItem "Civil Law Paralegal"
    .AddItem "Adverse Actions Paralegal"
    .AddItem "Other see notes"
  End With

  With UserForm1.ComboTSC

    .AddItem "B – initial upgrade to journeyman (5 level)"
    .AddItem "C – initial upgrade to craftsman (7 level; SSgt-select or above)"
    .AddItem "F – held prior 5 level; in upgrade to 5 level (retrainee)"
    .AddItem "G – held prior 7 level; in upgrade to 7 level (retrainee SSgt-select or above)"
    .AddItem "R – fully qualified"
    .AddItem "Other see notes"
 End With

 With UserForm1.txtdate
 txtdate.Value = Format(txtdate.Value, "dd/mm/yyyy")
 End With
 nCurrentRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
 TraverseData (nCurrentRow)

 End Sub

Welcome to SO.欢迎来到 SO。

You execute Dim nCurrentRow As Long but you don't assign any value to it, so default value is 0.您执行Dim nCurrentRow As Long但您没有为其分配任何值,因此默认值为 0。

And then you do a loop with nCurrentRow = nCurrentRow - 1 so nCurrentRow = -1 .然后你用nCurrentRow = nCurrentRow - 1所以nCurrentRow = -1做一个循环。

Your condition to end loop is:您结束循环的条件是:

Loop Until nCurrentRow = 1 Or Sheets(1).Cells(nCurrentRow, 1).Value = Me.txtname.Value and that condition can never be true. Loop Until nCurrentRow = 1 Or Sheets(1).Cells(nCurrentRow, 1).Value = Me.txtname.Value并且该条件永远不会为真。

The first time your Do...Loop executes, the value of nCurrentRow is -1 so you condition to end is:您的Do...Loop第一次执行时, nCurrentRow值为-1因此您要结束的条件是:

...Loop Until -1= 1 Or Sheets(1).Cells(-1, 1).Value = Me.txtname.Value...

Notice the part after the Or .注意Or之后的部分。 It's calling an impossible cell (row -1, column 1).它正在调用一个不可能的单元格(第 -1 行,第 1 列)。 That's not possible, so VBA raises error 1004.这是不可能的,因此 VBA 会引发错误 1004。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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