VBA用户窗体查找多个记录,显示并循环浏览

VBA用户窗体查找多个记录,显示并循环浏览,vba,excel,userform,Vba,Excel,Userform,我正在创建一个userform,它在其中搜索工作表上的唯一ID,并显示位于同一行中的关联数据 我已经使用了另一个StackOverflow问题的帮助,但它并不完全适合我 我正在搜索的唯一ID有多个数据集。我在下面得到的代码,单击find,会显示第一条找到的记录,并弹出一个消息框,告诉用户工作表中有多少条记录。单击OK后,userform关闭 我想编辑它,这样在单击OK之后,用户可以单击FindNext按钮,userform将显示与原始搜索匹配的所有其他记录 代码如下: Private Sub F

我正在创建一个userform,它在其中搜索工作表上的唯一ID,并显示位于同一行中的关联数据

我已经使用了另一个StackOverflow问题的帮助,但它并不完全适合我

我正在搜索的唯一ID有多个数据集。我在下面得到的代码,单击find,会显示第一条找到的记录,并弹出一个消息框,告诉用户工作表中有多少条记录。单击OK后,userform关闭

我想编辑它,这样在单击OK之后,用户可以单击FindNext按钮,userform将显示与原始搜索匹配的所有其他记录

代码如下:

Private Sub FindNext_Click()
    Dim nextCell As Range
    Set nextCell = Cells.FindNext(After:=ActiveCell)
    'FindNext loops round to the initial cell if it finds no other so we test for it
    If Not nextCell.Address(external:=True) = ActiveCell.Address(external:=True) Then
        updateFields anchorCell:=nextCell
    End If
End Sub

Private Sub Find_Click()
    Worksheets("Master").Activate
    Dim strFind As String
    Dim FirstAddress As String
    Dim rSearch As Range
    Set rSearch = Range("a1", Range("a65536").End(xlUp))
    Dim f      As Integer
    Dim c As Object

    strFind = Me.TextBox1.Value

    With rSearch
        Set c = .Find(strFind, LookIn:=xlValues)
        If Not c Is Nothing Then
            updateFields anchorCell:=c
            FirstAddress = c.Address
            Do
                f = f + 1
               Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> FirstAddress
            If f > 1 Then
                Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")

                    Case vbOK
                    Case vbCancel

                End Select
                Me.Height = frmMax

            End If
        Else: MsgBox strFind & " not listed"
        End If
    End With

End Sub


Private Sub updateFields(anchorCell As Range)
anchorCell.Select
With Me
    .TextBox2.Value = anchorCell.Offset(0, 2).Value
    .TextBox3.Value = anchorCell.Offset(0, 3).Value
    .TextBox4.Value = anchorCell.Offset(0, 4).Value
    .TextBox6.Value = anchorCell.Offset(0, 13).Value
    .TextBox7.Value = anchorCell.Offset(0, 14).Value
    .TextBox8.Value = anchorCell.Offset(0, 15).Value
    .TextBox9.Value = anchorCell.Offset(0, 16).Value
    .TextBox10.Value = anchorCell.Offset(0, 17).Value
    .TextBox11.Value = anchorCell.Offset(0, 18).Value
    .TextBox12.Value = anchorCell.Offset(0, 19).Value
    .TextBox13.Value = anchorCell.Offset(0, 20).Value
    .TextBox14.Value = anchorCell.Offset(0, 21).Value
    .TextBox20.Value = anchorCell.Offset(0, 22).Value
End With
End Sub
Private Sub FindNext\u Click()
Dim nextCell As范围
设置nextCell=Cells.FindNext(后面:=ActiveCell)
'FindNext循环到初始单元格,如果它没有找到其他单元格,那么我们测试它
如果不是nextCell.Address(外部:=True)=ActiveCell.Address(外部:=True),则
updateFields anchorCell:=nextCell
如果结束
端接头
私有子查找_单击()
工作表(“主控”)。激活
Dim strFind As字符串
将第一个地址设置为字符串
Dim R搜索范围
设置rSearch=范围(“a1”,范围(“a65536”)。结束(xlUp))
作为整数的Dim f
作为对象的dimc
strFind=Me.TextBox1.Value
有研究
Set c=.Find(strFind,LookIn:=xlValues)
如果不是,那么c什么都不是
updateFields anchorCell:=c
FirstAddress=c.地址
做
f=f+1
集合c=.FindNext(c)
循环而不是c为Nothing,c.Address为FirstAddress
如果f>1,则
选择Case MsgBox(“有”&f&“strFind、vbOKCancel或vbequipment或vbDefaultButton1、“多个条目”的实例”)
案例vbOK
案例vbCancel
结束选择
Me.Height=frmMax
如果结束
其他:MsgBox strFind&“未列出”
如果结束
以
端接头
专用子updateFields(anchorCell作为范围)
anchorCell。选择
和我一起
.TextBox2.Value=anchorCell.Offset(0,2).Value
.TextBox3.Value=anchorCell.Offset(0,3).Value
.TextBox4.Value=anchorCell.Offset(0,4).Value
.TextBox6.Value=anchorCell.Offset(0,13).Value
.TextBox7.Value=anchorCell.Offset(0,14).Value
.TextBox8.Value=anchorCell.Offset(0,15).Value
.TextBox9.Value=anchorCell.Offset(0,16).Value
.TextBox10.Value=anchorCell.Offset(0,17).Value
.TextBox11.Value=anchorCell.Offset(0,18).Value
.TextBox12.Value=anchorCell.Offset(0,19).Value
.TextBox13.Value=anchorCell.Offset(0,20).Value
.TextBox14.Value=anchorCell.Offset(0,21).Value
.TextBox20.Value=anchorCell.Offset(0,22).Value
以
端接头

感谢单击
查找下一行的代码使用了这样一个事实,即显示的最后一行被设置为当前选择(请参见
锚定单元格。在
更新字段中选择
)。问题是,在这些调用之间,用户可能选择了另一个单元格,甚至是另一个工作表,可能会发生运行时错误

我建议另一种方法,它只有两个功能,一个负责统计匹配项并启动搜索,另一个负责更新和下一步”

选项显式
“作为范围的专用锚点”跟踪最后显示的行
私有子查找_单击()
'仅显示匹配数,并将更新委托给FidNext
不算长
count=WorksheetFunction.CountIf(工作表(“主”).UsedRange.Columns(“A”),TextBox1.Value)
如果计数小于1,则
msgBox TextBox1.Value&“未列出”
FindNext.Enabled=False
出口接头
如果结束
FindNext.Enabled=True
设置锚点=工作表(“主”)范围(“A65536”)。结束(xlUp)
FindNext\单击“现在将工作委托给FindNext”
端接头
私有子查找下一步单击()
'负责更新用户表单并滚动到下一个字段
设置锚点=工作表(“主”).UsedRange.Columns(“A”).Find(TextBox1.Value,锚点)
TextBox2.Value=anchor.offset(0,2).Value
TextBox3.Value=anchor.offset(0,3).Value
TextBox4.Value=anchor.offset(0,4).Value
TextBox6.Value=anchor.offset(0,13).Value
TextBox7.Value=anchor.offset(0,14).Value
TextBox8.Value=anchor.offset(0,15).Value
TextBox9.Value=anchor.offset(0,16).Value
TextBox10.Value=anchor.offset(0,17).Value
TextBox11.Value=anchor.offset(0,18).Value
TextBox12.Value=anchor.offset(0,19).Value
TextBox13.Value=anchor.offset(0,20).Value
TextBox14.Value=anchor.offset(0,21).Value
TextBox20.Value=anchor.offset(0,22).Value
工作表(“主控”)。激活
anchor.EntireRow.Activate
端接头

FindNext\u Click
的代码使用了这样一个事实,即显示的最后一行被设置为当前选择(请参见
anchorCell.Select
in
updateFields
)。问题是,在这些调用之间,用户可能选择了另一个单元格或甚至另一个工作表,会发生运行时错误

我建议另一种方法,它只有两个功能,一个负责统计匹配项并启动搜索,另一个负责更新和下一步”

选项显式
“作为范围的专用锚点”跟踪最后显示的行
私有子查找_单击()
'仅显示匹配数,并将更新委托给FidNext
不算长
count=WorksheetFunction.CountIf(工作表(“主”).UsedRange.Columns(“A”),TextBox1.Value)
如果计数小于1,则
msgBox TextBox1.Value&“未列出”
FindNext.Enabled=False
出口接头
如果结束
FindNext.Enabled=True
设置锚点=工作表(“主”)范围(“A65536”)。结束(xlUp)
FindNext\单击“现在将工作委托给FindNext”
端接头
专用子FindNext_Cli
Option Explicit
Private anchor As Range ' keeps track of the last shown row

Private Sub Find_Click()
    ' Only Displays the number of matches and delegates the updating to FidNext
    Dim count As Long
    count = WorksheetFunction.CountIf(Worksheets("Master").UsedRange.Columns("A"), TextBox1.Value)
    If count < 1 Then
        msgBox TextBox1.Value & " not listed"
        FindNext.Enabled = False
        Exit Sub
    End If
    FindNext.Enabled = True
    Set anchor = Worksheets("Master").Range("A65536").End(xlUp)
    FindNext_Click ' Now delegate the work to FindNext
End Sub

Private Sub FindNext_Click()
'responsible of updating the userform and scrolling to the next field 
    Set anchor = Worksheets("Master").UsedRange.Columns("A").Find(TextBox1.Value, anchor)

    TextBox2.Value = anchor.offset(0, 2).Value
    TextBox3.Value = anchor.offset(0, 3).Value
    TextBox4.Value = anchor.offset(0, 4).Value
    TextBox6.Value = anchor.offset(0, 13).Value
    TextBox7.Value = anchor.offset(0, 14).Value
    TextBox8.Value = anchor.offset(0, 15).Value
    TextBox9.Value = anchor.offset(0, 16).Value
    TextBox10.Value = anchor.offset(0, 17).Value
    TextBox11.Value = anchor.offset(0, 18).Value
    TextBox12.Value = anchor.offset(0, 19).Value
    TextBox13.Value = anchor.offset(0, 20).Value
    TextBox14.Value = anchor.offset(0, 21).Value
    TextBox20.Value = anchor.offset(0, 22).Value

    Worksheets("Master").Activate
    anchor.EntireRow.Activate
End Sub