|
参与一下。。。
- Sub ykcbf() '//2024.3.29
- Set fso = CreateObject("scripting.filesystemobject")
- Set d = CreateObject("Scripting.Dictionary")
- Application.ScreenUpdating = False
- Set sh = ThisWorkbook.Sheets("Sheet1")
- p = ThisWorkbook.Path & ""
- For Each f In fso.GetFolder(p).Files
- If f.Name Like "*.xls" Then
- If InStr(f.Name, ThisWorkbook.Name) = 0 Then
- fn = Replace(fso.GetBaseName(f), "数据", "")
- Set wb = Workbooks.Open(f, 0)
- With wb.Sheets("Sheet1")
- arr = .UsedRange
- wb.Close False
- End With
- For i = 2 To UBound(arr)
- s = arr(i, 1) & "|" & fn
- d(s) = d(s) + arr(i, 2)
- Next
- End If
- End If
- Next f
- With sh
- r = .Cells(Rows.Count, 1).End(3).Row
- arr = .[a1].Resize(r, 5)
- For i = 2 To UBound(arr)
- s = arr(i, 1) & "|" & Replace(arr(1, 3), "合计金额", "")
- If d.exists(s) Then
- arr(i, 3) = d(s)
- End If
- s = arr(i, 2) & "|" & Replace(arr(1, 4), "合计金额", "")
- If d.exists(s) Then
- arr(i, 4) = d(s)
- End If
- arr(i, 5) = arr(i, 4) - arr(i, 3)
- Next
- .[a1].Resize(1, 5).Interior.Color = 49407
- With .[a1].Resize(r, 5)
- .Value = arr
- .Borders.LineStyle = 1
- .HorizontalAlignment = xlCenter '//列居中
- .VerticalAlignment = xlCenter
- End With
- End With
- Set d = Nothing
- Application.ScreenUpdating = True
- MsgBox "OK!"
- End Sub
复制代码
|
|