|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 清风竹- 于 2024-9-23 13:51 编辑
用了2个字典,请测试。
Sub 一维转二维()
Dim t As Date
t = Timer
Dim lastRow As Long, i&, j&
Dim ar(), n1, n2
Dim d1 As Object
Dim d2 As Object
With ActiveSheet
lastRow = .Cells(.Rows.Count, "A").End(3).Row
ar = .Range("A1:B" & lastRow).Value
End With
ReDim brr(1 To 1000, 1 To 1000) '根据需要,定义一个足够大的数组。
Set d1 = CreateObject("Scripting.Dictionary")
Set d2 = CreateObject("Scripting.Dictionary")
n1 = 1: n2 = 1
For i = 2 To UBound(ar)
If Not d1.Exists(ar(i, 1)) Then
n1 = n1 + 1
d1(ar(i, 1)) = n1
brr(n1, 1) = ar(i, 1)
End If
If Not d2.Exists(ar(i, 2)) Then
n2 = n2 + 1
d2(ar(i, 2)) = n2
brr(1, n2) = ar(i, 2)
End If
brr(d1(ar(i, 1)), d2(ar(i, 2))) = brr(d1(ar(i, 1)), d2(ar(i, 2))) + 1
Next i
With Sheet5
.Cells.ClearContents
.Range("a1").Resize(n1 + 1, n2 + 1) = brr
End With
MsgBox Format(Timer - t, "0.00") & "s"
End Sub
|
评分
-
1
查看全部评分
-
|