|
用一个字典解决问题的方案。
- Sub fyExcelVBA()
- Dim arr, brr, i%, j%
- Dim dic, dic1
- Application.DisplayAlerts = False
- arr = Range("A1").CurrentRegion
- Set dic = CreateObject("scripting.dictionary")
- For i = 1 To UBound(arr)
- If Not dic.exists(arr(i, 1)) Then
- dic(arr(i, 1)) = arr(i, 2)
- Else
- dic(arr(i, 1)) = dic(arr(i, 1)) & "|" & arr(i, 2)
- End If
- Next i
- Cells.ClearContents
- Range("a1").Resize(1, dic.Count) = dic.keys
- For i = 0 To dic.Count - 1
- item1 = VBA.Split(dic.items()(i), "|")
- Cells(2, 1 + i).Resize(UBound(item1) + 1, 1) = WorksheetFunction.Transpose(item1)
- Next i
- Set dic = Nothing
- End Sub
复制代码 |
|