1

我有一系列客户姓名。这个数组充满了重复,并且需要,因为订单行中的其他数据可能会有所不同。此数据没有唯一标识符,我需要将一个订单中的数据与数组中包含该客户的所有行进行比较。

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

任何帮助,将不胜感激!

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
4

2 回答 2

1

有几种方法可以做你想做的事。最简单的可能是使用像@Richard Morgan 在他对您的评论中建议的字典。

您还可以循环遍历数组并使用正确的名称创建一个新数组。

示例代码:

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

同样,我认为使用字典可能更好,但这应该可行。

于 2013-09-03T20:29:08.753 回答
1

哇,看起来好复杂,您是否尝试过在当前数组旁边创建一个表并使用公式:

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

Duplicates如果有重复则显示,如果没有则显示No Duplicates。当然是从 A2-A11。

或者为了让事情变得非常简单,您可以使用条件格式,输入类似的内容

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

对于仅出现一次的值的项目。您可以稍微修改以获得阵列的配色方案。然后在边上扔一个钥匙,告诉哪个颜色意味着多少重复。

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

于 2013-11-27T08:37:16.320 回答