需要它搜索重复记录,直到找到标准

时间:2014-09-27 11:56:15

标签: excel vba excel-vba

我仍然对VBA很陌生并且正在努力使用以下代码!

我要做的是让搜索功能查看第1列中的所有单元格,并找到Cell 1匹配PickerName2.Text的条件,偏移单元格5是&lt;&gt; 0然后偏移单元格6 = Blank < / p>

我遇到的问题是第1列一直有重复,一旦找到匹配的名称PickerName2然后它检查偏移单元格5&amp; 6,因为这些标准不符合标准,所以它给出了他们目前正在挑选的信息。

即使在表格的下方,也有一条符合标准的记录。我希望它查看所有记录,直到找到标准,或者如果它已经检查了A列中所有填充的单元格并且没有匹配,那么它将给出它们当前没有选择的消息。

我希望有人可以提供帮助: - )

的Al

Private Sub CommandButton3_Click()

Dim iRow As Long
    Dim ws As Worksheet
    Dim strSearch As String
    Dim aCell As Range
    Dim rng As Range, i As Long

    '~~> Set the sheet where you want to search the IMEI
    Set ws = Sheets("PickData")

        With ws
        '~~> Get the value which you want to search
        strSearch = PickerName2.Text

        '~~> Column A is Column 1 so Column B is 2. This is where we are searching
        '~~> xlWhole is used in the code below so that we find a complete match
        '~~> xlPart is supposed to be used when you are finding a partial match.
        Set aCell = .Columns(1).Find(What:=strSearch, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)


            '~~> get the row of the cell where we found the match and add data to

          If Not aCell Is Nothing And aCell.Offset(0, 5).Value <> "" And aCell.Offset(0, 6).Value = "" Then
          MsgBox " " & PickerName2.Text & " is currently picking - " & aCell.Offset(0, 1) & " " & aCell.Offset(0, 2) _
          & " " & aCell.Offset(0, 3) _
          & " "
          UserForm1.Hide

          Else
          MsgBox " " & PickerName2.Text & " has no outstanding PIK!", vbExclamation
          PickerName2.Value = ""
          Exit Sub
           End If

    End With
    PickNo2.Value = ""
    PickerName2.Value = ""
    UserForm1.Hide

End Sub

我已经在网上进行了一些搜索并尝试了类似于以下代码的内容但是仍然遇到了一个需要的对象错误,但却看不到我错在哪里?

Dim rng As Range
Dim i As Integer
Dim finalrow As Integer

finalrow = Sheets("PickData").Range("A10000").End(xlUp).Row

    For i = 2 To finalrow
    If Cells(i, 1).Value = UserForm1.PickerName2.Text And (Cell.Offset(i, 5) <> "") And (Cell.Offset(i, 6) = "") Then
      MsgBox " " & PickerName2.Text & " is currently picking - " & Cell.Offset(i, 1) & " " & Cell.Offset(i, 2) _
      & " " & Cell.Offset(i, 3) _
      & " "
      End If
    Next i

1 个答案:

答案 0 :(得分:1)

使用Range.Find方法进行首次代码尝试是个不错的开始。 现在使用Range.FindNext方法扩展此方法。

This link可以帮到你。

在你的第二段代码中:

Cell.Offset ...的引用应该类似于Cell(i,1).Offset(0,5)和Cells(i,1).Offset(0,1)以及Cells(i,1).Offset(0, 3)分别。

对PickerName2的第二次引用应该使用UserForm1限定,如UserForm1.PickerName2