|
楼主 |
发表于 2022-11-26 10:31
|
显示全部楼层
再来复习下老师的经典代码
Sub fbhz()
myPath = ThisWorkbook.Path & "\"
myFile = Dir(myPath & "*.xls*")
Dim br(1 To 65536, 1 To 256)
Application.ScreenUpdating = False
Set d1 = CreateObject("scripting.dictionary")
Set d2 = CreateObject("scripting.dictionary")
Do While myFile <> ""
If myFile <> ThisWorkbook.Name Then
Set sh = Workbooks.Open(myPath & myFile)
ar = sh.Sheets(1).[a1].CurrentRegion
br(1, 1) = ar(1, 1)
For i = 2 To UBound(ar, 2)
gjz = ar(1, i)
If Not d1.exists(gjz) Then
k1 = k1 + 1
d1(gjz) = k1 '记录列数
br(1, d1(gjz) + 1) = gjz
End If
Next
For i = 2 To UBound(ar)
gjz = ar(i, 1)
If Not d2.exists(gjz) Then
k2 = k2 + 1
d2(gjz) = k2
br(d2(gjz) + 1, 1) = gjz
For j = 2 To UBound(ar, 2)
If ar(i, j) > 0 Then br(d2(gjz) + 1, d1(ar(1, j)) + 1) = br(d2(gjz) + 1, d1(ar(1, j)) + 1) + ar(i, j)
Next
Else
For j = 2 To UBound(ar, 2)
If ar(i, j) > 0 Then br(d2(gjz) + 1, d1(ar(1, j)) + 1) = br(d2(gjz) + 1, d1(ar(1, j)) + 1) + ar(i, j)
Next
End If
Next
Workbooks(myFile).Close False
End If
myFile = Dir
Loop
ThisWorkbook.Sheets(1).[k1].Resize(k2 + 1, k1 + 1) = br
Application.ScreenUpdating = True
End Sub
|
|