留下买路财2008 留下买路财2008
关注数: 14 粉丝数: 25 发帖数: 2,836 关注贴吧数: 78
像各位大佬们求助,我想知道我这个VBA哪里错了,拜谢 Sub 批量合并() Dim FolderPath As String Dim File As String Dim wbTarget As Workbook Dim wbSource As Workbook Dim wsSource As Worksheet Dim lastRow As Long Dim i As Long Dim fileCount As Integer Application.DisplayAlerts = False With Application.FileDialog(msoFileDialogFolderPicker) .Title = "请选择包含Excel文件的文件夹" If .Show = -1 Then FolderPath = .SelectedItems(1) If FolderPath = "" Then Exit Sub End With File = Dir(FolderPath & "\*.xls*") Do While File <> "" Set wbTarget = ThisWorkbook Set wbSource = Workbooks.Open(FolderPath & "\" & File) Set wsSource = wbSource.Worksheets("A") lastRow = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row For i = 4 To lastRow wsSource.Rows(i).Copy Destination:=wbTarget.Worksheets("A").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) Next i Set wsSource = wbSource.Worksheets("B") lastRow = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row For i = 4 To lastRow wsSource.Rows(i).Copy Destination:=wbTarget.Worksheets("B").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) Next i wbSource.Close SaveChanges:=False fileCount = fileCount + 1 File = Dir() Loop MsgBox "处理了 " & fileCount & " 个Excel文件" End Sub '很简单的功能,就是想批量把目标文件夹里的EXCEL文件表A和表B从第4行到最后一行复制分别到本文件表A和表B的最后一行,执行后能复制过来,但是整个WPS就卡住了,点什么都不行,只能强制关进程,是哪里没写对吗?
1 下一页