1

我最初有一个 Excel 电子表格,用于记录 JOB LOT ID (>10000) 的值。我使用了以下数组公式 -

=INDIRECT(TEXT(MIN(IF(($C$3:$S$52<>"")*(COUNTIF($V$3:V3,$C$3:$S$52)=0),ROW($3:$52)*100+COLUMN($C$S),7^8)),"R0C00"),)&""

数据:

| pallet    | Lot#      | Lot#      | Lot#      | Lot#      | Lot#      |
|--------   |-------    |-------    |--------   |-------    |--------   |
| 1         | 12345     | 12346     | 12345     | 12347     | 123456    |
| 2         | 12345     | 12346     | 12348     | 12348     | 12343     |
| 3         | 12345     | 12347     | 123456    | 12348     | 12348     |

如果该范围内的单元格都代表 JOB LOT ID,则此方法可以正常工作。当我将其复制到结果范围并结合计数公式时,它将记录唯一的 LOT #

(IF(LEN(V4)>0,COUNTIF($C$3:$S$52,V4),"")

在相邻的单元格中。它返回:

Unique  
Value  Count
______ _____
12345    4   
12346    2   
12347    2  
123456   2 
12348    4 
12343    1

不幸的是,工作和电子表格的范围发生了变化,因此电子表格需要在每个工作批次单元格之前包含列,以记录工作批次的案例#。

我需要帮助的是弄清楚如何忽略 case# 数据,它总是在 1 到 451 之间,并且只计算唯一的 JOB LOT ID,它总是 > 100000。导致只有唯一的 Job Numbers 列表. 使用添加了 Case# 列的相同数组公式,当不需要或不需要时,还会列出案例编号。

| pallet    | case#     | Lot#      | case#     | Lot#      | case#     | Lot#      | case#     | Lot#      | case#     | Lot#      |
|--------   |-------    |-------    |-------    |-------    |-------    |--------   |-------    |-------    |-------    |--------   |
| 1         | 1         | 12345     | 45        | 12346     | 356       | 12345     | 6         | 12347     | 7         | 123456    |
| 2         | 3         | 12345     | 35        | 12346     | 212       | 12348     | 23        | 12348     | 200       | 12343     |
| 3         | 54        | 12345     | 34        | 12347     | 450       | 123456    | 345       | 12348     | 367       | 12348     |

结果是

Unique
Value   Count
______  _____
12345     4  
45        1
12346     2 
356       1
6         1
12347     2 
7         1  
123456    2 
35        1 
212       1 
12348     4 
23        1 
200       1 
12343     1 
34        1 
450       1 
345       1 
367       1

有什么建议吗?谢谢。

4

1 回答 1

0

您可以使用字典来保存唯一Lot#性作为键,并在每次再次遇到该键时向与该键关联的值添加一个。

数据从工作表中,从 C 列到最右列,读入一个数组,arr. arr循环仅查看每隔一列,即Lot#列。字典的内容,即唯一的Lot#( Keys) 和它们的计数 ( Items),被写入 sheet2。

它假设您的数据从 A1 开始并具有问题中给出的布局。

Option Explicit
Public Sub GetUniqueValueByCounts()
    Dim arr(), i As Long, j As Long, dict As Object, lastColumn As Long, lastRow As Long
    Const NUMBER_COLUMNS_TO_SKIP = 2
    Set dict = CreateObject("Scripting.Dictionary")

    With ThisWorkbook.Worksheets("Sheet1")
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        If lastColumn < 3 Or lastRow < 2 Then Exit Sub

        arr = .Range(.Cells(2, 3), .Cells(lastRow, lastColumn)).Value

        For i = LBound(arr, 2) To UBound(arr, 2) Step NUMBER_COLUMNS_TO_SKIP
            For j = LBound(arr, 1) To UBound(arr, 1)
               If arr(j, i) <> vbNullString Then
                   dict(arr(j, i)) = dict(arr(j, i)) + 1
               End If
            Next
        Next
    End With
    With Worksheets("Sheet2")
        .Range("A1").Resize(dict.Count, 1) = Application.WorksheetFunction.Transpose(dict.Keys)
        .Range("B1").Resize(dict.Count, 1) = Application.WorksheetFunction.Transpose(dict.Items)
    End With
End Sub

排序结果:

您可以使用 asortedList来获得有序的结果,尽管您失去了一次性生成数组的好方法.Keys.Items方法以写入工作表。

Option Explicit
Public Sub GetUniqueValueByCounts()
    Dim arr(), i As Long, j As Long, dict As Object, lastColumn As Long, lastRow As Long, list As Object
    Const NUMBER_COLUMNS_TO_SKIP = 2
    Set dict = CreateObject("Scripting.Dictionary")
    Set list = CreateObject("System.Collections.SortedList")
    With ThisWorkbook.Worksheets("Sheet1")
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        If lastColumn < 3 Or lastRow < 2 Then Exit Sub

        arr = .Range(.Cells(2, 3), .Cells(lastRow, lastColumn)).Value

        For i = LBound(arr, 2) To UBound(arr, 2) Step NUMBER_COLUMNS_TO_SKIP
            For j = LBound(arr, 1) To UBound(arr, 1)
                If arr(j, i) <> vbNullString Then
                    With list
                        If Not .contains(arr(j, i)) Then
                            list.Add arr(j, i), 1
                        Else
                            list(arr(j, i)) = list(arr(j, i)) + 1
                        End If
                    End With
                End If
            Next
        Next i
    End With
    With Worksheets("Sheet2")
        For j = 0 To list.Count - 1
            .Cells(j + 1, 1) = list.GetKey(j)
            .Cells(j + 1, 2) = list.GetByIndex(j)
        Next
    End With
End Sub
于 2018-08-29T14:18:41.417 回答