|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
'原始表格为当前工作表A1开始,如左图
'输出当前工作表D2开始
Option Explicit
Sub test()
Dim dic(1), i, arr, t, m, key
For i = 0 To UBound(dic)
Set dic(i) = CreateObject("scripting.dictionary")
Next
arr = [a1].CurrentRegion
For i = 2 To UBound(arr, 1)
If Not dic(0).exists(arr(i, 1)) Then
m = m + 1: dic(0)(arr(i, 1)) = m
End If
If dic(1).exists(arr(i, 2)) Then
t = dic(1)(arr(i, 2))
ReDim Preserve t(UBound(t) + 1)
t(UBound(t)) = dic(0)(arr(i, 1))
dic(1)(arr(i, 2)) = t
Else
dic(1)(arr(i, 2)) = Array(dic(0)(arr(i, 1)))
End If
Next
ReDim arr(1 To dic(0).Count + 1, 1 To dic(1).Count + 1): m = 1
For Each key In dic(0).keys: m = m + 1: arr(m, 1) = key: Next
m = 1
For Each key In dic(1).keys
m = m + 1: t = dic(1)(key)
arr(1, m) = key
For i = 0 To UBound(t): arr(t(i) + 1, m) = "flag": Next
Next
With Range("d2")
.Resize(UBound(arr, 1), UBound(arr, 2)) = arr
.Offset(1, 1).Resize(UBound(arr, 1) - 1, UBound(arr, 2) - 1). _
Interior.ColorIndex = xlNone
For Each key In .Offset(1, 1).Resize(UBound(arr, 1) - 1, UBound(arr, 2) - 1)
If key = "flag" Then key.Interior.Color = vbRed: key.ClearContents
Next
End With
End Sub |
|