|
- Sub gg()
- Dim fso, fs, f, i%, j%, m%, pa$, wb, dat, n%, ar, br, t, r%, p%, jp
- Set fso = CreateObject("scripting.filesystemobject")
- pa = ThisWorkbook.Path
- Set fs = fso.getfolder(pa)
- m = Workbooks(ThisWorkbook.Name).Sheets(1).[a1].End(4).Row
- ReDim ar(1 To m, 1 To 1)
- Application.ScreenUpdating = False
- For Each f In fs.Files
- If f.Name Like "*.xlsx" And Not (f.Name Like "~$*") Then
- Set wb = Workbooks.Open(f)
- dat = [a1].CurrentRegion
- wb.Close
- Set wb = Nothing
- n = [a1].End(xlToRight).Column + 1
- Cells(1, n) = Split(f.Name, ".")(0)
- ReDim br(1 To UBound(dat), 1 To n)
- r = 1
- For i = 2 To UBound(dat)
- t = 1
- For j = 2 To m
- If dat(i, 1) = Cells(j, 1) Then
- ar(j - 1, 1) = dat(i, 3)
- t = 0
- Exit For
- End If
- Next j
- If t Then
- For p = 1 To 4
- If p < 3 Then
- br(r, p) = dat(i, p)
- ElseIf p = 3 Then
- br(r, p) = dat(i, p + 1)
- ElseIf p = 4 Then
- br(r, p) = "=sum(e" & j + r - 1 & ":rr" & j + r - 1 & ")"
- End If
- Next p
- br(r, n) = dat(i, 3)
- t = 1
- r = r + 1
- End If
- Next i
- Cells(2, n).Resize(UBound(ar), 1) = ar
- Cells(m + 1, 1).Resize(UBound(br), n) = br
- End If
- Next f
- Set jp = Sheet1.[a1].CurrentRegion
- jp.Sort key1:="定额编号", Header:=xlYes
- Set jp = Nothing
- Set fso = Nothing
- Set fs = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|