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

问题描述

我不是经验丰富的编码员,所以请原谅我的无知。我写了一段代码,循环浏览来自在线请求表单的响应,并将结果保存到电子表格中。

我有几个问题,第一个是如果我在结果中加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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...