|
楼主 |
发表于 2024-7-10 19:53
|
显示全部楼层
经过反复测试,添加下面红色代码后,可以实现我的想法。谢谢老师们
Sub 按J1更新分表()
startTime = Timer
Application.ScreenUpdating = False
Dim WbName, wa, wb, ar, br, i&, j&, r&, s&, m&, n&, v&, w&, x&, y&
Set wa = thisWorkbook: s = [h2]: ar = Range("e5:j" & s)
WbName = Array("0 三同.xlsm", "1 组三.xlsm", "2 组六.xlsm")
If Cells(1, "J") >= 0 And Cells(1, "J") < 3 Then
x = Cells(1, "J"): y = x
ElseIf Cells(1, "J") = 3 Then x = 0: y = Cells(1, "J") - 1
Else: MsgBox "J1 输入有误!": Exit Sub
End If
For w = x To y
thisWorkbook.Activate
If Cells(1, "J") = 3 Then Workbooks.Open (thisWorkbook.Path & "\" & WbName(w))
For Each wb In Workbooks
ReDim br(1 To UBound(ar), 1 To 6)
If wb.Name = WbName(w) Then
wb.Activate: Sheets("总表").Activate
With Sheets("总表")
v = .[i1]: n = .[h1]: m = 0
For i = 1 To UBound(ar)
If ar(i, 6) = v Then
m = m + 1
r = Application.Match(v, wa.Sheets("总表").Range("j5:j" & s), 0)
For j = 1 To 5: br(m, j) = ar(i, j): Next
End If
Next
For i = 1 To m
If i = 1 Then br(i, 6) = r Else br(i, 6) = br(i, 1) - br(i - 1, 1)
Next
br(m + 1, 6) = .[c1] - br(m, 1)
Range(.Cells(5, "e"), .Cells(n, "j")).ClearContents
.Cells(5, "e").Resize(m + 1, 6).NumberFormat = "@"
.Cells(5, "e").Resize(m + 1, 6) = br
Erase br
wb.Save: GoTo t
End With
End If
Next
MsgBox "" & WbName(w) & " 未打开!"
t: Next
thisWorkbook.Activate
Application.ScreenUpdating = True
MsgBox "运行时间:" & Timer - startTime & " 秒"
End Sub
|
|