|
![](https://clubstatic.excelhome.net/image/common/ico_lz.png)
楼主 |
发表于 2017-2-19 16:03
|
显示全部楼层
Sub 字典查询返回唯一值并有排序()
Dim arr, brr, x&, y&, z&, d As Object
Application.ScreenUpdating = False
Set d = CreateObject("Scripting.Dictionary")
Set dk = CreateObject("Scripting.Dictionary")
With Sheets("数据")
arr = .Range("a1").CurrentRegion
For x = 2 To UBound(arr)
s = arr(x, 3) & "+" & arr(x, 5) & "+" & arr(x, 8)
If Not d.exists(s) Then
d(s) = Array(arr(x, 10), arr(x, 15), arr(x, 16))
End If
Next x
End With
dnum = Sheet1.[b1].Value
For i = 2 To UBound(arr)
If arr(i, 5) = dnum Then
If Not dk.exists(arr(i, 3)) Then
dk(arr(i, 3)) = ""
End If
End If
Next i
On Error Resume Next
Sheet1.Activate
Range("d2:az2").ClearContents
Range("d4:az10000").ClearContents
dname = dk.keys: k = 4
For j = 0 To dk.Count
Cells(2, k) = dname(j)
k = k + 3
Next
brr = Range("a1").CurrentRegion
For y = 4 To UBound(brr)
For z = 4 To UBound(brr, 2) Step 3
ss = brr(2, z) & "+" & brr(1, 2) & "+" & brr(y, 2)
If d.exists(ss) Then
Cells(y, z).Resize(1, 3) = d(ss)
End If
Next z
Next y
End Sub |
|