VBA循环。查找更新“ After”值

时间:2018-11-09 11:33:12

标签: excel vba

在遍历.Find时是否可以更新'After'值?

我有一个包含多组数据的工作表,而我基本上是在尝试寻找一种将.Find用于多个条件的方法。

示例数据:

|Pet     |Colour     |Treats     |
|Cat     |Black      |1          |
|Cat     |Black      |2          |
|Cat     |Black      |3          |
|Cat     |White      |1          |
|Cat     |White      |2          |
|Cat     |White      |3          |
|Dog     |Black      |1          |
|Dog     |Black      |2          |
|Dog     |Black      |3          |
|Dog     |White      |1          |
|Dog     |White      |2          |
|Dog     |White      |3          |

代码:

Dim foundCell As Range
Set foundCell = .Range("A2")
MsgBox "Pre foundCell = " & foundCell.Value

For Each x In Array(Pet, Colour)
Set search = .Find(What:=x, After:=foundCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
        foundCell.Cells = Range(search.Address) '<---- doesnt work
        MsgBox "Post foundCell = " & foundCell.Value
Next x

因此,如果我搜索“黑狗”,则第一个循环会找到“狗”(在A8处),但随后重置为“黑色”(B2)颜色的第一个出现,因此最后我得到了错误的行。 我希望更新“ foundCell”值并将其用作第二个循环开始的点,但它似乎总是重置回原始的Cell值。

1 个答案:

答案 0 :(得分:0)

这是您如何操作的基本版本。
这适用于给定的示例-例如,如果您拥有所有的白狗,然后又有几只黑鹦鹉,它将返回不正确的结果-它仅检查颜色是否在找到该动物的行之后出现。

您可以更改搜索范围的大小,以便它可以找到Dog并仅搜索dog范围内的颜色。这需要在第二条FIND语句之前完成,但是在代码确认已找到动物之后才进行。 Resize应该能够处理此问题,以及找到的动物的数量和找到该动物的返回行号。 编辑:已将其添加到代码中。

第二个FIND仅使用几个参数(要查找的内容和从何处开始查找),而其他参数则从前一个FIND继承而来。

Edit2:这将返回动物出现的第一行,然后返回出现在动物范围内的颜色的第一行-因此,如果找到一种颜色,它将是该颜色那只动物。这需要对“宠物”列进行排序,因为如果您在该列表的下方还有其他猫狗,它将返回错误的动物计数。

Sub Test()

    Dim rFoundAnimal As Range
    Dim rFoundColour As Range
    Dim rSearchRange As Range
    Dim Animal As String
    Dim Colour As String

    Dim AnimalCount As Long

    Animal = "Dog"
    Colour = "Black"

    With ThisWorkbook.Worksheets("Sheet4")
        Set rSearchRange = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)) 'Search Range A1:A13.
    End With
    With rSearchRange
        Set rFoundAnimal = rSearchRange.Find(What:=Animal, _
                                             After:=.Cells(1, 1), _
                                             LookIn:=xlValues, _
                                             LookAt:=xlWhole, _
                                             SearchOrder:=xlByRows, _
                                             SearchDirection:=xlNext, _
                                             MatchCase:=False)
    End With
    'Animal has been found.
    If Not rFoundAnimal Is Nothing Then
        AnimalCount = WorksheetFunction.CountIf(rSearchRange, Animal)
        Set rSearchRange = rFoundAnimal.Offset(, 1).Resize(AnimalCount)
        With rSearchRange
            'Start search at bottom of list - it wraps around so first searched cell is at top of list.
            Set rFoundColour = .Find(What:=Colour, After:=.Cells(.Rows.Count, 1))
        End With
        'Colour has been found.
        If Not rFoundColour Is Nothing Then
            MsgBox "Animal:  " & rFoundAnimal.Address & vbCr & _
                   "Colour:  " & rFoundColour.Address
        Else
            MsgBox "Animal: " & rFoundAnimal.Address & vbCr & _
                   "Colour not found."
        End If
    Else
        MsgBox "Animal not found."
    End If

End Sub