|
楼主 |
发表于 2020-11-13 14:42
|
显示全部楼层
Sub 按钮1_Click()
Application.ScreenUpdating = False
Dim Fld, F
Dim newname, pa, newpath As String '要汇总的文件所在的目录及名称
Dim arr As Variant '用来存储各文件中的数据
Dim mrow As Long '用来存储各工作表中需要汇总的数据行数
Dim c As Range '汇总表中A列的第一空单元格
pa = ThisWorkbook.path
Set Fld = CreateObject("Scripting.FileSystemObject").GetFolder(pa)
For Each F In Fld.Files
If F.Name Like "*.xlsx" Then
Workbooks.Open pa & "\" & F.Name '打开指定的工作簿
newname = [a2]
''''''''''''''''''''''''''''复制合并'''''''''''''''''''''''''''''''''''''''''''''''''''
'确定要汇总的数据行数
mrow = ActiveWorkbook.Worksheets(1).UsedRange.Rows.Count - 1
'将要汇总的数据存储到变量中
arr = ActiveWorkbook.Worksheets(1).Range("A2").Resize(mrow, 5).Value
'获得汇总表中A列的第一个非空单元格
Set c = ThisWorkbook.Worksheets(1).Range("A1048576").End(xlUp).Offset(1, 0)
'将数组中数据写入汇总表
c.Resize(mrow, 5) = arr
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''创建文件夹'''''''''''''''''''''''''''''''''''''''''''''''''
If Dir(pa & "\" & newname, vbDirectory) = "" Then '判断是否存在同名文件夹
MkDir pa & "\" & newname
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''另存重命令工作簿'''''''''''''''''''''''''''''''''''''''''''''''''
mypath = pa & "\" & newname
ActiveWorkbook.SaveAs mypath & "\" & newname & ".xlsx"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ActiveWorkbook.Close False
End If
Next
Application.ScreenUpdating = True
End Sub
|
|