VBA UserForm查找多个logging,显示和循环

我正在创build一个用户表单,在表单上search唯一的ID,并显示位于同一行的关联数据。

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

有多个数据集到我正在寻找的唯一ID。 下面的代码,点击查找,显示第一个find的logging,并popup一个消息框,告诉用户在工作表中有多lesslogging。 点击确定后,用户表单closures。

我想编辑它,所以单击确定后,用户可以单击FindNextbutton,用户窗体将显示所有其他匹配原始search的logging。

以下是代码:

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 

谢谢

FindNext_Click的代码使用最后一行显示被设置为当前select的事实(请参阅updateFields anchorCell.Select )。 问题是,在这些调用之间,用户可能已经select了另一个单元甚至另一个工作表,则会发生运行时错误。

我build议另一种方法只有两个function,一个是计数匹配,并启动search,另一个是负责更新和nexting“

 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