在具有重复值的数组中查找值

我有一系列的客户名称。 这个数组充满了重复项,并且需要这样,因为顺序行中的其他数据可能会有所不同。 这个数据没有唯一的标识符,我需要比较一个订单的数据和数组中有这个客户的所有行。

我无法让for循环search所有有客户匹配的行。

任何帮助,将不胜感激!

Dim prodlog As String Dim orddate As Variant Dim cus As String Dim owner As String Dim orddate2 As Variant Dim owner2 As String Dim LogCusts As Variant LogCusts = Application.Transpose(Range("F3", Range("F" & Rows.count).End(xlUp))) Dim loglen As Integer loglen = UBound(LogCusts) - LBound(LogCusts) Dim cust2 As Variant For Each cust2 In LogCusts Dim custrow As Integer custrow = Application.Match(cust2, LogCusts, False) + 1 prodlog = Range(Cells(custrow, 5), Cells(custrow, 5)).Value orddate = Range(Cells(custrow, 2), Cells(custrow, 2)).Value cus = Range(Cells(custrow, 6), Cells(custrow, 6)).Value owner = Range(Cells(custrow, 7), Cells(custrow, 7)).Value databook.Activate logjam.Select orddate2 = Range(Cells(custrow + 1, 5), Cells(custrow + 1, 5)).Value owner2 = Range(Cells(custrow + 1, 7), Cells(custrow + 1, 7)).Value If IsEmpty(orddate) Then Exit For End If If IsEmpty(prodlog) Then trackbook.Activate masterlog.Select Range(Cells(custrow, 2), Cells(custrow, 17)).Clear Else: While cus = cust2 If orddate = orddate2 And owner = owner2 Then Range(Cells(custrow, 8), Cells(custrow, 8)).Value = prodlog End If Wend End If Next cust2 

有几种方法可以做你想做的事情。 最简单的方法就是使用@Richard Morgan在他给你的评论中build议的字典。

您也可以循环访问数组,并使用正确的名称创build一个新数组。

示例代码:

 Private Sub UniqueArray(oldData as Variant) as Collection Set UniqueArray = New Collection Dim i as integer Dim j as integer Dim foundFlag as boolean For i = 1 to oldData.Count foundFlag = False FOr j = i + 1 to oldData.Count If oldData(i) = oldData(j) then foundFlag = True End If Next j If not foundFlag then UniqueArray.Add oldData(i) Next 

再次,我认为使用字典可能更好,但这应该工作。

哇,看起来如此复杂,你有没有尝试创build一个表旁边的当前数组,并使用公式:

 =IF(MAX(COUNTIF(A2:A11,A2:A11))>1,"Duplicates","No Duplicates") 

如果有Duplicates则显示Duplicates如果No Duplicates则显示No Duplicates 。 从A2-A11当然。

或者为了保持简单,你可以使用条件格式,input类似的东西

 =COUNTIF($B$2:$B$11,B2)=1 

对于只出现一次的值的项目。 您可以稍微修改以获取arrays的配色scheme。 然后扔在一边的钥匙,告诉哪个颜色意味着有多less重复。

不知道这是否有帮助,但我尽可能远离VBA。