【问题标题】:Userform Command Button Next (Matching Criteria)用户窗体命令按钮下一步(匹配标准)
【发布时间】:2026-01-20 23:40:01
【问题描述】:

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

我有几个问题,第一个是行计数不包括第一行,如果我在结果中加 1(这是正确的,很好!!!)

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

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 行)

【问题讨论】:

  • LRow 未声明也未定义。它应该是什么?它是一个公共变量吗?
  • 是的,LRow 已被声明为公开。它用于许多 Subs 或 Functions
  • 如何确定?它的值是否正确?您可以发布数据样本吗?
  • 这是 LRow 的代码 - LRow = xRequest.Cells(Rows.Count, 1).End(xlUp).Row

标签: excel vba for-loop userform commandbutton


【解决方案1】:

遵循您的代码有点困难,因为其中一些代码在其他地方被调用并设置这些模块中使用的变量。我创建了一个通用代码块来查找下一个不是“是”的值。我做了一些假设,并试图在代码的 cmets 中将它们清除。

基本上,我有一列包含我们正在循环的值,寻找一个不是 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

【讨论】:

  • 对不起,这可能是我的经验不足。这用于填充大量用户表单字段,我将如何处理。我刚刚将代码应用于我的表单,但是当我尝试读取数据时它失败了。我的经验不足表明我很抱歉
  • @Welshdpm 无需道歉。我正在更新我上面的帖子以演示它如何在用户表单中工作。我创建了一个带有两个文本框的简单用户表单,并使用数据表中 B 列和 C 列中的相应值填充这些文本框。尝试使其适应您的工作簿,然后反馈。
最近更新 更多