|
本帖最后由 chzsh 于 2022-12-25 07:27 编辑
Sub Macro1()
Dim MyPath$, MyName$, sh As Worksheet, arr
MyPath = ThisWorkbook.Path & "\销售\"
MyName = Dir(MyPath & "*.xls")
Application.ScreenUpdating = False
j3 = ThisWorkbook.Sheets(1).Range("a65536").End(xlUp).Row
ThisWorkbook.Sheets(1).Range("b6:b" & j3).ClearContents
ThisWorkbook.Sheets(1).Range("d6:d" & j3).ClearContents
ThisWorkbook.Sheets(1).Range("h6:h" & j3).ClearContents
ThisWorkbook.Sheets(1).Range("j6:j" & j3).ClearContents
Do While MyName <> ""
If MyName <> ThisWorkbook.Name Then
Set wb = GetObject(MyPath & MyName)
If wb.Name = "表1.xls" Then
For i = 2 To wb.Sheets.Count
k = wb.Sheets(i).Range("i65536").End(xlUp).Row
m = wb.Sheets(1).Range("i65536").End(xlUp).Row
wb.Sheets(i).Range("a4:i" & k).Copy wb.Sheets(1).Range("a" & m + 1)
m = m + k - 3
Next
arr = wb.Sheets(1).Range("a4:i" & m)
Set d = CreateObject("scripting.dictionary")
For i = 1 To UBound(arr)
s = arr(i, 9)
If Not d.exists(s) Then
d(s) = Array(Val(arr(i, 4)), arr(i, 6))
Else
d(s) = Array(Val(arr(i, 4)) + d(s)(0), Val(arr(i, 6)) + d(s)(1))
End If
Next
k = d.keys
t = d.items
With ThisWorkbook
For j1 = 6 To j3
For j2 = 0 To d.Count - 1
If .Sheets(1).Range("a" & j1) = k(j2) Then
.Sheets(1).Range("b" & j1) = t(j2)
.Sheets(1).Range("d" & j1) = t(j2)(1)
End If
Next j2
Next j1
End With
End If
If wb.Name = "表2.xls" Then
For i = 2 To wb.Sheets.Count
k = wb.Sheets(i).Range("a65536").End(xlUp).Row
m = wb.Sheets(1).Range("a65536").End(xlUp).Row
wb.Sheets(i).Range("a4:j" & k).Copy wb.Sheets(1).Range("a" & m + 1)
m = m + k - 3
Next
arr = wb.Sheets(1).Range("a4:j" & m)
Set d = CreateObject("scripting.dictionary")
For i = 1 To UBound(arr)
s = arr(i, 1)
If Not d.exists(s) Then
d(s) = Array(Val(arr(i, 4)), arr(i, 5))
Else
d(s) = Array(Val(arr(i, 4)) + d(s)(0), Val(arr(i, 5)) + d(s)(1))
End If
Next
k = d.keys
t = d.items
With ThisWorkbook
For j1 = 6 To j3
For j2 = 0 To d.Count - 1
If .Sheets(1).Range("a" & j1) = k(j2) Then
.Sheets(1).Range("h" & j1) = t(j2)
.Sheets(1).Range("j" & j1) = t(j2)(1)
End If
Next j2
Next j1
End With
End If
wb.Close False
MyName = Dir
End If
Loop
Application.ScreenUpdating = True
MsgBox "ok"
End Sub
|
评分
-
1
查看全部评分
-
|