|
挺好玩的,凑个数。正表之外的也是按先列后行提取。
- Sub test()
- Set d = CreateObject("scripting.dictionary")
- Set d1 = CreateObject("scripting.dictionary")
- With Worksheets("sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- c = .Cells(1, .Columns.Count).End(xlToLeft).Column
- arr = .Range("a1").Resize(r, c)
- End With
- ReDim brr(1 To 1000, 1 To 7)
- ReDim crr(1 To 1000, 1 To 7)
- For i = 2 To UBound(arr)
- x = arr(i, 5)
- If Not d.exists(x) Then d(x) = arr(i, 4) Else d(x) = Application.Min(d(x), arr(i, 4)) '取最小数量
- For j = 7 To UBound(arr, 2) 'd1(组 & 列)=对应行的集合
- If arr(i, j) <> "" Then d1(x & j) = d1(x & j) & "," & i
- Next
- Next
-
- For Each x In d.keys '每组
- jj = d(x) + 6 '最小数量对应的最小列
- For j = 7 To UBound(arr, 2)
- t = d1(x & j) 'd1(组 & 列)=对应行的集合
- If Len(t) Then
- trr = Split(Mid(t, 2), ",")
- If j <= jj Then '<=最小列的,提取到一张表
- For Each i In trr
- m = m + 1
- For k = 1 To 6: brr(m, k) = arr(i, k): Next
- brr(m, 7) = arr(i, j)
- Next
- Else '>最小列的,提取到另一张表
- For Each i In trr
- n = n + 1
- For k = 1 To 6: crr(n, k) = arr(i, k): Next
- crr(n, 7) = arr(i, j)
- Next
- End If
- End If
-
- Next
- Next
- With Sheets("最终") '按序输出
- If m > 0 Then .[I2].Resize(m, 7) = brr
- If n > 0 Then .Cells(m + 2, "I").Resize(n, 7) = crr
- End With
- End Sub
复制代码 |
|