Excel宏循环遍历行并显示是否找到某个值,然后在单击按钮时继续

时间:2017-11-02 11:19:44

标签: vba excel-vba excel

如果它与来自不同工作表的单元格值匹配,我想在userform中显示某行的某些值。一旦我点击一个按钮并显示下一个匹配的记录,我需要能够继续搜索。这是我到目前为止的代码

Private Sub NextRecord_Click()

Dim wst1 As Worksheet
Dim lastRowE As Integer
Dim lastRowF As Integer
Dim lastRowM As Integer
Dim foundTrue As Boolean

' stop screen from updating to speed things up
Application.ScreenUpdating = False

lastRowE = Sheets("Raw Data (USSD Dials)").Cells(Sheets("Raw Data (USSD Dials)").Rows.Count, "C").End(xlUp).Row
lastRowF = Sheets("Raw Data (Merchant Bills)").Cells(Sheets("Raw Data (Merchant Bills)").Rows.Count, "H").End(xlUp).Row


For i = 1 To lastRowE
foundTrue = False
For j = 1 To lastRowF

If Sheets("Raw Data (USSD Dials)").Cells(i, 2).Value = Sheets("Raw Data (Merchant Bills)").Cells(j, 8).Value Then
    foundTrue = True
    Exit For
End If


AgentInterface.CustNameTextBox = Sheets("Raw Data (Merchant Bills)").Cells(j, 2).Value & " " & Sheets("Raw Data (Merchant Bills)").Cells(j, 3)
AgentInterface.AccNoTextBox = Sheets("Raw Data (Merchant Bills)").Cells(j, 7).Value
AgentInterface.CustNumTextBox = Sheets("Raw Data (Merchant Bills)").Cells(j, 1).Value
AgentInterface.DueDateTextBox = Sheets("Raw Data (Merchant Bills)").Cells(j, 4).Value
AgentInterface.MSISDNTextBox = Sheets("Raw Data (Merchant Bills)").Cells(j, 8).Value
AgentInterface.ServiceNameTextBox = Sheets("Raw Data (Merchant Bills)").Cells(j, 5).Value


Next j


Next i

' stop screen from updating to speed things up`enter code here`
Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

一种方法是跟踪Sub

执行之间最后找到的行
Option Explicit

Private Sub NextRecord_Click()
    Dim wsDials As Worksheet, lrDials As Long, i As Long, j As Long
    Dim wsBills As Worksheet, lrBills As Long, agent As Object
    Static lastDials As Long, lastBills As Long

    Set wsDials = ThisWorkbook.Worksheets("Raw Data (USSD Dials)")
    Set wsBills = ThisWorkbook.Worksheets("Raw Data (Merchant Bills)")
    lrDials = wsDials.Cells(wsDials.Rows.Count, "C").End(xlUp).Row
    lrBills = wsBills.Cells(wsBills.Rows.Count, "H").End(xlUp).Row
    Set agent = AgentInterface

    If lastDials = 0 Then lastDials = 1         'first run only (lastDials is static)
    If lastBills = 0 Then lastBills = 1
    If lastDials > lrDials Then lastDials = 1   'last run only (beyond UsedRange)
    If lastBills > lrBills Then lastBills = 1
    For i = lastDials To lrDials
        For j = lastBills To lrBills
            If wsDials.Cells(i, "C").Value = wsBills.Cells(j, "H").Value Then
                lastDials = i + 1
                lastBills = j + 1
                With wsBills
                    agent.CustNameTextBox = .Cells(j, "B").Value & " " & .Cells(j, "C").Value
                    agent.AccNoTextBox = .Cells(j, "G").Value
                    agent.CustNumTextBox = .Cells(j, "A").Value
                    agent.DueDateTextBox = .Cells(j, "D").Value
                    agent.MSISDNTextBox = .Cells(j, "H").Value
                    agent.ServiceNameTextBox = .Cells(j, "E").Value
                End With
                Exit Sub
            End If
        Next
    Next
End Sub

Sub使用2个静态变量“记住”多次运行之间最后找到的行

  • 仅在值为0(首次执行)时初始化它们
  • 当它们的值大于它们各自的最后一行时重新初始化
  • 如果找到匹配,则会记住这两行,并且退出两个循环(因此为子)
  • 关闭ScreenUpdating并没有获得任何感知效果。事实上,对于这么快的操作,当你点击按钮时它实际上可能会引起一些闪烁,所以我把它删除了

我不确定AgentInterface是什么,但如果它是UserForm,你应该声明它是

  • Dim agent As UserForm(或任何对象类型)从早期绑定中受益
相关问题