'按文件名命名规则做了升序,随机测试了一下汇总数据都没有问题。
'源文件"合计"标志必须在A列,缺少源文件就猜到这里了。
Option Explicit
Sub abc()
Dim a, i, f, n, m, p, w, pth, filename(1 To 10 ^ 4, 1 To 3), cnt
pth = ThisWorkbook.Path
If Not getfilename(filename, cnt, pth, ".xls") Then MsgBox "!": Exit Sub
Call doevent(False)
Cells.ClearContents
For i = 1 To cnt
Set w = Workbooks.Open(filename(i, 1))
With w
With .ActiveSheet
a = .[a1].CurrentRegion.Value
If IsArray(a) Then
m = m + 1
If m = 1 Then
.[a1].Resize(UBound(a), UBound(a, 2)).Copy _
ThisWorkbook.ActiveSheet.[a1]
Else
p = ThisWorkbook.ActiveSheet.Cells(Rows.Count, "a").End(xlUp).Row + 1
.[a1].Offset(3).Resize(UBound(a) - 3, UBound(a, 2)).Copy _
ThisWorkbook.ActiveSheet.Cells(p, "a")
End If
Else
Debug.Print filename(i, 1)
End If
End With
.Close False
End With
Next
For i = Cells(Rows.Count, "a").End(xlUp).Row To 4 Step -1
If InStr(Cells(i, "a").Value, "合计") > 0 Then Rows(i).Delete
Next
Call doevent(True)
End Sub
Function getfilename(filename, cnt, pth, mark) As Boolean
Dim f, i, t, p
If right(pth, 1) <> "\" Then pth = pth & "\"
f = Dir(pth & "*.*")
Do While Len(f) > 0
If LCase(right(f, Len(mark))) = LCase(mark) Then
If left(f, 1) <> "~" Then
cnt = cnt + 1
filename(cnt, 1) = pth & f
If InStr(f, "[") Then
t = Split(f, "[")
filename(cnt, 2) = t(0)
If InStr(t(1), "_") Then
t = Split(t(1), "_")
If IsNumeric(t(0)) Then
filename(cnt, 3) = Val(t(0))
Else
MsgBox pth & f: End
End If
Else
MsgBox pth & f: End
End If
Else
MsgBox pth & f: End
End If
End If
End If
f = Dir
Loop
If cnt > 0 Then
Call bsort(filename, 1, cnt, 1, 3, 2)
For i = 1 To cnt
If filename(i, 2) <> filename(i + 1, 2) Then
Call bsort(filename, p + 1, i, 1, 3, 3)
p = i
End If
Next
getfilename = True
End If
End Function
Function bsort(a, first, last, left, right, key)
Dim i, j, k, t
For i = first To last - 1
For j = first To last + first - 1 - i
If a(j, key) > a(j + 1, key) Then
For k = left To right
t = a(j, k): a(j, k) = a(j + 1, k): a(j + 1, k) = t
Next
End If
Next j, i
End Function
Function doevent(flag As Boolean)
With Application
.DisplayAlerts = flag
.ScreenUpdating = flag
End With
End Function
2022年05月06日 03点05分
13
大神,数据都正确的按顺序提取出来了,但是楼上我贴的那图中,合计行下面的制表人那一行分别出现在了数据当中。辛苦大神能不能把这个完善一下,目前就差这一点了。
2022年05月06日 07点05分
我把 If InStr(Cells(i, "a").Value, "合计") > 0 Then Rows(i).Delete,中的合计改成制表人,加在后,再次合并数据,完美了。十分感谢VBA大佬。
2022年05月06日 07点05分