【问题标题】:Next and Previous Button - Breaking my mental state下一个和上一个按钮 - 打破我的精神状态
【发布时间】:2020-03-12 02:20:06
【问题描述】:

我已经尝试了几天来获得一个在 excel VBA 表单中工作的下一步按钮。我有一个包含多个项目的表单,包括一些组合框。我反复尝试让代码正常工作,这样我就可以添加下一个项目按钮和上一个项目按钮。我希望这些按钮搜索表格数据并显示上一组数据和下一组数据。最终,一旦按钮工作,就可以编辑这些。上一个按钮将我带到第一组数据,但不会循环,下一个按钮在我的行名中出现对象错误(txt.name.text = cells(currentrow,1) 代码。获取这些按钮的任何帮助或帮助工作将不胜感激。

Private Sub cmdnext_Click()
  Dim lastrow As Long
  Dim currentrow As Long

lastrow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row

currentrow = currentrow + 2

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

If currentrow = lastrow Then
MsgBox "Why wont this work"
End If
End Sub

Private Sub CommandButton6_Click()
Dim lastrow As Long
 Dim currentrow As Long

 If lastrow = currentrow Then
   MsgBox "this doesn work yet"
 End If

 currentrow = currentrow - 2

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

 End Sub

【问题讨论】:

    标签: excel vba button


    【解决方案1】:

    好吧,我可以告诉你为什么CommandButton6_Click 不起作用。 currentrow 的初始值为零,然后减去 2 并将其用作单元格行索引。

    使用Cells(-2, something)不会有好的结局。


    而且,现在我仔细观察,您对cmdnext_Click 也有同样的问题。因为每次运行此函数时currentrow 都从零开始,并且您向其添加两个,所以您总是得到Cells(2,something)


    我认为您可能需要找到一种方法使currentrow 持久化,这样每次单击按钮时它就不会被初始化为零。

    【讨论】:

      【解决方案2】:

      类似这样的:

      Dim currentrow As Long '<< you should set a value for this
                             '   when your form first loads
      
      Private Sub LoadRow()
          With Sheet1.Rows(currentrow)
              txtname = .Cells(1)
              txtposition = .Cells(2)
              txtassigned = .Cells(3)
              '... etc etc
              txtcle.Text = .Cells(20)
          End With
      End Sub
      
      Private Sub cmdnext_Click()
          If currentrow < Sheet1.Cells(Rows.Count, 2).End(xlUp).Row Then
              currentrow = currentrow + 2
              LoadRow
          Else
              MsgBox "Already at end"
          End If
      End Sub
      
      Private Sub cmdPrev_Click()
          If currentrow > 2 Then
              currentrow = currentrow - 2
              LoadRow
          Else
              MsgBox "Already at top"
          End If
      End Sub
      

      无需重复所有加载代码。

      【讨论】:

      • 谢谢。我试过了,它没有给我任何错误,但上一个按钮只返回到第一行。我试图让它循环遍历所有行。有什么建议吗?
      • 是的 - 尝试调试,看看哪里出了问题。您可能需要在表单加载时添加一些代码,将currentRow 定位到您希望加载的第一个记录的任何位置。
      • 谢谢它的工作。现在开始使用更新按钮。
      猜你喜欢
      • 1970-01-01
      • 2013-12-14
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多