|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
0.zip
(1.39 MB, 下载次数: 3)
代码如下:
Sub 按J1更新分表()
sj = 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) = br
Erase br
wb.Save: GoTo t
End With
End If
Next
MsgBox "" & WbName(w) & "未打开!"
t: Next
ThisWorkbook.Activate
Application.ScreenUpdating = True
MsgBox "运行时间:" & Timer - sj & " 秒"
End Sub
这是个按《00 总表》J列指定数值0,1,2,复制粘贴E:I列对应行数据到《0 三同》,《1 组三》,《2 组六》的VBA过程。
由于《00 总表》E列是数值格式,F:I列是文本格式;而复制粘贴的目标工作簿《0 三同》,《1 组三》,《2 组六》的E:H列则是数值格式,导致提取的数据与源工作簿《00 总表》不一致,无法进行下一步计算。
希望老师们:在上面代码合适的地方,添加复制粘贴到目标工作簿《0 三同》,《1 组三》,《2 组六》E:I列的符合条件的所有数据,保留《00 总表》E:I列原有数字格式的代码。
谢谢
|
|