下一步用户窗体命令按钮(匹配条件)

Userform Command Button Next (Matching Criteria)

我不是一个有经验的编码员,所以请原谅我的无知。我编写了一段代码,循环处理在线申请表的回复,结果保存到电子表格中。

我遇到了几个问题,第一个是行计数不包括第一行,如果我在结果中加 1(这是正确的,没关系!!!)

当我 运行 表单根据条件 Not x = "Yes" 显示第一条记录时,但是当我单击下一步时,它将循环到下一行,然后停止。如果我将行数加 1,它会转到最后一行。

Private Sub UserForm_Initialize()

Call SetVariables

Dim Count As Long
Dim ReqRow As Long

    For Count = 2 To LRow
        If Not xRequest.Range("AF" & Count).Value = "Yes" Then
            Me.TB_Requester = xRequest.Range("F" & Count).Value
            Me.TB_Email = xRequest.Range("D" & Count)
            
            ReqRow = Count
            Exit For
        End If
    Next Count

'These are just recording the Row and Count 
'Me.TB_PropAction = ReqRow
'Me.TB_UsageScore = LRow

End Sub

Private Sub CmdB_Next_Click()

Call SetVariables

Dim Count As Long
Dim Record As Long

With xRequest
If Record = 0 Then Record = 1

    For Count = (Record + 1) To LRow Step 1
        If Not .Range("AF" & Count).Value = "Yes" Then
            Me.TB_Requester = .Range("F" & Count).Value
            Me.TB_Email = .Range("D" & Count)
            
            ReqRow = Count
        End If
    Next Count
    
    If (Count - 1) = LRow Then
        MsgBox "End of Component Submissions"
    End If
    
End With

Me.TB_PropAction = ReqRow

End Sub

谁能告诉我哪里出错了,我在电子表格中只有 6 行,它应该循环显示 3 个请求,但无论我做什么我只得到 2 个(第 3 和 4 行或第 3 和6)

跟踪您的代码有点困难,因为其中一些代码在别处被调用并设置这些模块中使用的变量。我创建了一个通用代码块来查找下一个不是“是”的值。我做了一些假设,并试图在代码的注释中澄清它们。

基本上,我有一个列,其中包含我们正在循环的值,以查找不是 Yes 的值。至于“当前”记录是什么,我假设它是电子表格中的选定单元格。如果存储在别处的是不同的东西,那么改变逻辑来支持它应该不会太难。查看下面的代码是否为您指明了正确的方向,或者您是否需要其他帮助。

Sub Test()
    Dim looper As Long
    Dim lastRow As Long
    
    lastRow = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
    
    'Loop through this range from the 'Current' row
    'until we find the next desired value.
    'Note: I can't really tell how you store the current row.
    'Are you actually using selected cell as a record pointer,
    'or is there a form-level variable holding that information?
    
    looper = Selection.Row
    
    Do
        looper = looper + 1
        If Sheets("Data").Range("A" & looper).Value <> "Yes" Then
            Exit Do
        End If
    Loop While looper <= lastRow

    'If the looping variable is greater than the last row
    'then we are outside of the used range.
    'We can cleanup and exit
    If looper > lastRow Then
        MsgBox "Finished"
        Me.TextBox1.Value = ""
        Me.TextBox2.Value = ""
        Exit Sub
    End If
    
    'Populate controls with found value
    Me.TextBox1.Value = Sheets("Data").Range("B" & looper).Value
    Me.TextBox2.Value = Sheets("Data").Range("C" & looper).Value
    
    'Move the record pointer to this found row
    Sheets("Data").Range("A" & looper).Select
    
End Sub