我正在尝试使用Excel宏创建一个简单的表单。此表单将用于输入将存储在Sheet2中的数据。如果用户想要查看已保存的数据,他们应该能够使用“下一步”和“上一步”按钮进行导航。我试图编写这个逻辑,虽然我能够加载第一条记录,之后我无法移动到下一条记录。下面是我用于导航到下一条记录的代码。
- 用于在Sheet2中保存数据的代码。
Private Sub Save_Click()
Dim RowCount As Long
Dim ctl As Control
RowCount = Worksheets("Sheet2").Range("A1").CurrentRegion.Rows.Count
With Worksheets("Sheet2").Range("A1")
.Offset(RowCount, 0).Value = Me.Name1.Value
.Offset(RowCount, 1).Value = Me.Account.Value
.Offset(RowCount, 3).Value = Format(Now, "dd/mm/yyyy hh:nn:ss") 'date when the record was created
ActiveWorkbook.Save
End With
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
ctl.Value = ""
End If
Next ctl
End Sub
- 导航到下一条记录的代码
Private Sub Next1_Click()
i = i + 1: j = 1
If i > (Sheets("Sheet2").Rows.Count - 4) Then
MsgBox "Max rows Reached"
Exit Sub
End If
Set rng = Worksheets("Sheet2").Range("A1")
Name1.Text = rng.Offset(i, j).Value: j = j + 1
Account.Text = rng.Offset(i, j).Value: j = j + 1
End Sub
答案 0 :(得分:0)
在评论中与OP讨论后,很明显问题的原因是未知的变量i
。您应该使用以下构造来使其工作:
Public i As Long
Private Sub Save_Click()
'your code here
End Sub
Private Sub Next1_Click()
'your code here
End Sub