|
比较复杂,写起来费时。。。- Sub ykcbf() '//2024.9.26
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Dim tm: tm = Timer
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set d = CreateObject("Scripting.Dictionary")
- Set d1 = CreateObject("Scripting.Dictionary")
- p = ThisWorkbook.Path & ""
- p1 = p & "领料单"
- p2 = p & "产品"
- For Each f In fso.GetFolder(p1).Files
- If LCase(f.Name) Like "*.xls*" Then
- If InStr(f, "~$") = 0 Then
- If InStr(f, ThisWorkbook.Name) = 0 Then
- fn = fso.GetBaseName(f)
- s = Replace(Split(fn, "-")(0), "领料", "")
- Set wb = Workbooks.Open(f, 0)
- arr = wb.Sheets(1).UsedRange
- wb.Close 0
- d1(s) = Array(arr(2, 4), arr(2, 6))
- End If
- End If
- End If
- Next
- For Each k In d1.keys
- d.RemoveAll
- x = 0
- ReDim brr(1 To 10000, 1 To 100)
- m = 4: n = 7: bt = [{"数量","金额"}]
- For Each f In fso.GetFolder(p1).Files
- If LCase(f.Name) Like "*.xls*" Then
- If InStr(f, "~$") = 0 Then
- If InStr(f, ThisWorkbook.Name) = 0 Then
- fn = fso.GetBaseName(f)
- rq = Split(fn, "-")(1)
- If InStr(fn, k) Then
- If Not d.exists(rq) Then
- n = n + 2
- d(rq) = n
- brr(4, n - 1) = bt(1)
- brr(4, n) = bt(2)
- brr(3, n - 1) = rq
- End If
- x = x + 1
- Set wb = Workbooks.Open(f, 0)
- arr = wb.Sheets(1).UsedRange
- wb.Close 0
- If x = 1 Then
- For j = 1 To 6
- brr(2, j) = arr(2, j)
- brr(3, j) = arr(4, j)
- Next
- brr(3, 6) = "合计": brr(4, 6) = "数量": brr(4, 7) = "金额"
- brr(2, 2) = k: brr(2, 4) = d1(k)(0): brr(2, 6) = d1(k)(1)
- End If
- For i = 5 To UBound(arr)
- If Val(arr(i, 1)) Then
- s = arr(i, 2)
- If Not d.exists(s) Then
- m = m + 1
- d(s) = m
- brr(m, 1) = m - 4
- brr(m, 2) = s
- brr(m, 3) = arr(i, 3)
- brr(m, 4) = arr(i, 4)
- brr(m, 5) = arr(i, 5)
- End If
- r = d(arr(i, 2))
- brr(r, n - 1) = arr(i, 6)
- brr(r, n) = arr(i, 8)
- End If
- Next
- End If
- End If
- End If
- End If
- Next
- Application.SheetsInNewWorkbook = 1
- Set wb = Workbooks.Add
- With wb.Sheets(1)
- .[a2].Resize(1, n).Interior.ColorIndex = 6
- .[a3].Resize(2, n).Interior.Color = 49407
- .Name = k
- With .[a1].Resize(m, n)
- .Value = brr
- .Borders.LineStyle = 1
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- .[a1].Resize(2, n).Borders.LineStyle = 0
- For i = 5 To UBound(arr)
- sum1 = 0: sum2 = 0
- For j = 8 To n
- If .Cells(4, j) = "数量" Then
- sum1 = sum1 + .Cells(i, j).Value
- End If
- If .Cells(4, j) = "金额" Then
- sum2 = sum2 + .Cells(i, j).Value
- End If
- Next
- .Cells(i, 6).Value = sum1
- .Cells(i, 7).Value = sum2
- Next
- For j = 6 To n Step 2
- .Cells(3, j).Resize(, 2).Merge
- Next
- For j = 1 To 5
- .Cells(3, j).Resize(2).Merge
- Next
- End With
- wb.SaveAs p2 & k
- wb.Close
- Next
- Set d = Nothing
- Set d1 = Nothing
- Application.ScreenUpdating = True
- MsgBox "共用时:" & Format(Timer - tm) & "秒!"
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|