|
不就想知道我有没有实现吗,代码给你,自己去验证呗
- Sub test2()
- Dim Dic, Dict, Arr, Arrt, Tmp, N&, I&, K&, P&, S$, mTimer#, T#
- mTimer = Timer: T = Timer
- Set Dic = CreateObject("scripting.dictionary")
- Set Dict = CreateObject("scripting.dictionary")
- Debug.Print "创建字典用时:"; Timer - T: T = Timer
- With ThisWorkbook
- With .Worksheets("Raw Data"): Arr = .Range("a2:b" & .Cells(.Rows.Count, 1).End(3).Row).Formula: End With
- Debug.Print "读取数据用时:"; Timer - T: T = Timer
- For N = LBound(Arr) To UBound(Arr)
- If Dic(Arr(N, 1)) = "" Then
- Dic(Arr(N, 1)) = vbBack & Arr(N, 2) & vbBack
- ElseIf InStr(Dic(Arr(N, 1)), vbBack & Arr(N, 2) & vbBack) = 0 Then
- Dic(Arr(N, 1)) = Dic(Arr(N, 1)) & Arr(N, 2) & vbBack
- End If
- Next N
- Debug.Print "整理数据用时:"; Timer - T: T = Timer
- Arrt = Dic.Keys
- For N = LBound(Arrt) To UBound(Arrt)
- Arr = Split(Dic(Arrt(N)), vbBack)
- If UBound(Arr) - LBound(Arr) > 2 Then
- For I = LBound(Arr) + 1 To UBound(Arr) - 2
- K = I
- For P = I + 1 To UBound(Arr) - 1
- If StrComp(Arr(K), Arr(P)) = 1 Then K = P
- Next P
- If K <> I Then S = Arr(I): Arr(I) = Arr(K): Arr(K) = S
- Next I
- End If
- S = Join(Arr, "||")
- If Dict(S) = "" Then
- Dict(S) = vbBack & Arrt(N) & vbBack
- ElseIf InStr(Dict(S), vbBack & Arrt(N) & vbBack) = 0 Then
- Dict(S) = Dict(S) & Arrt(N) & vbBack
- End If
- Next N
- Debug.Print "提取结果用时:"; Timer - T: T = Timer
- P = 0: K = 0
- Arr = Dict.Keys: ReDim Arrt(LBound(Arr) + 1 To UBound(Arr) + 1, 1 To 20)
- For N = LBound(Arr) To UBound(Arr)
- Tmp = Split(Dict(Arr(N)), vbBack)
- If UBound(Tmp) - LBound(Tmp) > 2 Then
- K = K + 1: Arrt(K, 1) = Mid(Arr(N), 3)
- If UBound(Tmp) > P Then P = UBound(Tmp)
- For I = LBound(Tmp) + 1 To UBound(Tmp) - 1
- If I + 1 > UBound(Arrt, 2) Then ReDim Preserve Arrt(LBound(Arrt) To UBound(Arrt), 1 To UBound(Arrt, 2) + 20)
- Arrt(K, I + 1) = Tmp(I)
- Next I
- End If
- Next N
- Debug.Print "生成结果用时:"; Timer - T: T = Timer
- .Worksheets("result").[f2].Resize(K, P).Value = Arrt
- Debug.Print "写出结果用时:"; Timer - T
- End With
- Set Dic = Nothing: Set Dict = Nothing
- Debug.Print "程序运行用时:"; Timer - mTimer
- Debug.Print "结果数量:"; K
- End Sub
复制代码 |
|