【问题标题】:Next and Previous Buttons in Excel VBA FormExcel VBA 表单中的下一个和上一个按钮
【发布时间】:2020-06-23 04:12:33
【问题描述】:

我正在处理一个表单,它将收集一组数据。我有文本框、组合框和几个选项按钮。我尝试了多种方法来创建正确的代码以转到上一条记录和下一条记录,但我似乎无法使代码正常工作。我是新手,但希望有人可以提供帮助。如果有帮助,我也可以通过电子邮件发送表格。我尝试添加 TraverseData 来清理并使其更容易输入数据。我似乎让下一个按钮工作,但我不断收到 1004 错误,上一个按钮循环回到 TraverseData。我的代码如下,非常感谢任何帮助。

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

【问题讨论】:

  • 欢迎来到 SO。你执行Dim nCurrentRow As Long,但你没有给它赋值,所以默认值为0。然后你用nCurrentRow = nCurrentRow - 1循环,所以nCurrentRow = -1。你结束循环的条件是Loop Until nCurrentRow = 1 Or Sheets(1).Cells(nCurrentRow, 1).Value = Me.txtname.Value,这个条件永远不会是真的。

标签: excel vba forms button next


【解决方案1】:

欢迎来到 SO。

你执行了Dim nCurrentRow As Long,但你没有给它赋值,所以默认值为0。

然后你用nCurrentRow = nCurrentRow - 1 循环,所以nCurrentRow = -1

您结束循环的条件是:

Loop Until nCurrentRow = 1 Or Sheets(1).Cells(nCurrentRow, 1).Value = Me.txtname.Value 并且该条件永远不会为真。

您的Do...Loop 第一次执行时,nCurrentRow 的值是-1,所以您结束的条件是:

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

注意Or 后面的部分。它正在调用一个不可能的单元格(第 -1 行,第 1 列)。这是不可能的,所以 VBA 会引发错误 1004。

【讨论】:

  • 谢谢,所以基本上我需要为 nCurrentRow 创建一个值,这样公式就不会搜索行 -1 我可以输入 nCurrentRow = nCurrentRow +1 吗?
  • 是的,nCurrentRow = nCurrentRow +1 将在第一个循环中返回 1,在第二个循环中返回 2,以此类推。我不知道这是否正是你需要让它工作的,但它确实可以避免错误 1004
  • 我试试看。我试图让它通过以前的记录,以便以后可以重新编辑,而无需直接进入 excel
猜你喜欢
  • 2012-08-05
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 2014-04-13
  • 1970-01-01
相关资源
最近更新 更多