Sub test()
Dim mAry, i As Long, mRow As Long, wb1 As Workbook
Dim wb As Workbook, mPath As String, mFn As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
If Workbooks.Count > 1 Then MsgBox "关闭其他工作簿后重试!": Exit Sub
'------------设置搜索路径-----------------
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "--------------------------------------请选择源数据文件所在的文件夹-------------------"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count = 0 Then MsgBox "你放弃了操作": Exit Sub
mPath = .SelectedItems(1)
End With
Workbooks.Add
Set wb1 = ActiveWorkbook
wb1.SaveAs mPath & "\结果" & Format(Now, "yyyymmddhhmmss") & ".xlsx", xlOpenXMLWorkbook
'-------------遍历文件,收集符合要求的数据-----------------
mFn = Dir(mPath & "\*.csv")
Do While mFn <> ""
If mFn <> ThisWorkbook.Name And Left(mFn, 2) <> "结果" Then
Set wb = Workbooks.Open(mPath & "\" & mFn)
mAry = wb.Worksheets(1).[a1].CurrentRegion
wb.Close 0
With wb1.Worksheets(1)
mRow = .Cells(.Rows.Count, 1).End(3).Row
mRow = IIf(mRow = 1, 1, mRow + 1)
.Cells(mRow, 1).Resize(UBound(mAry, 1), UBound(mAry, 2)) = mAry
End With
End If
mFn = Dir
Loop
wb1.Save
MsgBox "处理完成!"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
说明一下,按要求,是什么意思?
如果就是要个思路,那就简单了,
循环遍历数据所在文件夹,
导入数据,再按你的要求整理数据就好了。