使用两个查找findnext

我还不熟悉在VBA中使用range / find / findnext,并寻求一些帮助。 我已经成功地实现了单一的find – > findnext循环,但我想知道是否有可能使用findnext与两个发现。

我使用的代码如下。 为了简单起见,我正在使用披萨库存。

要点是我有一个电子表格,第一列是分类栏(奶酪,肉,素食),而H栏(偏移部分)是types – (奶酪,Parmesean,意大利辣香肠等)。 我想要做的是在某一列中编译一个不同types的列表,而不需要重复。

例如,我想在第一列中find“奶酪”一词的所有实例。我希望代码先find奶酪这个词,然后给我一个types(切达芝士等)。 第二个发现是检查我的最后一列中是否已经存在这种types。 如果它已经出现在列中(第二个查找函数=不是什么),那么它将转到下一个“Cheese”。 如果它是新的(第二个查找function=没有),那么我想它把types在列中,并继续。

因为它是错误的,并考虑到语法几乎相同,我以前的Find / Findnext,只增加了一个额外的FindNext,我意识到它可能是引用错误的。 有什么办法可以避免这种情况?

注:我意识到我可以很容易地填写所有types的最后一列,删除重复,然后sorting,但这似乎有点不雅和费时。 我想知道是否有一个更方便的方式沿着我尝试的路线。

a = 2 Set c = Range("I:I").Find("Cheese") firstadd = c.Address If Not c Is Nothing Then Do temp = c.Offset(0, -1) Set d = Range("BA:BA").Find(temp) If d Is Nothing Then Cells(a, 53) = temp a = a + 1 End If Set c = Range("I:I").FindNext(c) Loop While Not c Is Nothing And c.Address <> firstadd End If End Function 

尝试一个For Each循环来避免使用find / findnext循环。

 a = 2 For Each c in Range("I:I") If InStr(c.Value, "Cheese") Then temp = c.Offset(0, -1).Value If Not Range("BA:BA").Find(temp) Is Nothing Then Cells(a, 53).Value = temp a = a + 1 End If End If Next c 

尝试这个。

 Dim exists As Integer a = 2 Set c = Range("I:I").Find("Cheese") firstadd = c.Address Do While Not c Is Nothing And c.Address <> firstadd temp = c.Offset(0, -1) exists = WorksheetFunction.CountIf(Range("BA:BA"), temp) If exists > 0 Then Cells(a, 53) = temp a = a + 1 End If Set c = Range("I:I").FindNext(c) Loop End Function