我正嘗試使用Excel宏創建一個簡單的窗體。此表單將用於輸入將存儲在Sheet2中的數據。如果用戶想要查看已經保存的數據,他們應該能夠使用「下一步」和「上一步」按鈕進行導航。我試圖編寫這個邏輯,儘管我能夠加載第一個記錄,之後我無法移動到下一個記錄。以下是我用於導航到下一條記錄的代碼。使用excel在行之間導航宏
- 用於在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
你的'i'變量是什麼?它被宣佈爲「公共」嗎? –
是的,它被宣佈爲公開... – user3175856
你檢查了你的值是否在sheet2中保存正確? –