|
Sub 一维转二维()
Dim d As Object, dc As Object
Set d = CreateObject("scripting.dictionary")
Set dc = CreateObject("scripting.dictionary")
Dim ar As Variant
Dim br()
r = Cells(Rows.Count, 16).End(xlUp).Row
If r < 5 Then MsgBox "数据源为空!": End
ar = Range("p4:r" & r)
For i = 2 To UBound(ar)
If ar(i, 2) <> "" Then
dc(ar(i, 2)) = ""
End If
Next i
ReDim br(1 To UBound(ar), 1 To dc.Count + 1)
k = 1: y = 1
br(1, 1) = "地区"
For i = 2 To UBound(ar)
If ar(i, 1) <> "" And ar(i, 2) <> "" Then
t = d(ar(i, 1))
If t = "" Then
k = k + 1
d(ar(i, 1)) = k
t = k
br(k, 1) = ar(i, 1)
End If
lh = d(ar(i, 2))
If lh = "" Then
y = y + 1
d(ar(i, 2)) = y
lh = y
br(1, y) = ar(i, 2)
End If
br(t, lh) = ar(i, 3)
End If
Next i
[a1].CurrentRegion.Offset(3) = Empty
[a4].Resize(k, y) = br
MsgBox "ok!"
End Sub
|
|