|
原先代码如下:
Sub 批量导入多个工作簿的表到一个工作簿里()
Dim currWorkbook As Workbook
Dim shtCurr, shtLast As Worksheet
Dim strFilter, strTmpSheetName As String
Dim isHuiZong As Boolean
Dim vbMsgResult As VbMsgBoxResult
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set currWorkbook = ActiveWorkbook
Set shtCurr = ActiveSheet
Set shtLast = shtCurr
isHuiZong = False
strFilter = "Excel Files(*.xl*),*.x1*"
selectWorkbooks = Application.GetOpenFilename(strFilter, Title:="请选择要合并工作簿:", MultiSelect:=True)
On Error Resume Next
vbMsgResult = MsgBox("导入到工作簿点击‘是’,汇总到一个表里点击‘否’ ? ", vbExclamation + vbYesNoCancel)
Select Case vbMsgResult
Case vbNo
isHuiZong = True
Case vbCancel
Exit Sub
End Select
For Each eachWorkbook In selectWorkbooks
If eachWorkbook <> False Then
Set OpenBook = Workbooks.Open(eachWorkbook)
For Each xiSheet In OpenBook.Sheets
strTmpSheetName = OpenBook.Name & "_" & xiSheet.Name
For Each tmpSheet In currWorkbook.Sheets
If tmpSheet.Name = strTmpSheetName Then
tmpSheet.Delete
Exit For
End If
Next
xiSheet.Copy After:=shtLast
currWorkbook.Sheets(xlSheet.Name).Name = strTmpSheetName
Next
OpenBook.Close SaveChanges:=False
Set shtLast = currWorkbook.Sheets(strTmpSheetName)
End If
Next
shtCurr.Select
If isHuiZong = False Then
MsgBox "导入到工作簿成功!", vbExclamation
Else
汇总多表到一个表
For Each tmpSheet In currWorkbook.Sheets
If tmpSheet.Name <> shtCurr.Name Then
tmpSheet.Delete
End If
Next
End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Sub 汇总多表到一个表()
Dim shtHui As Worksheet
Dim maxRow, maxRow_1 As Long
Dim strExName As String
Set shtHui = ActiveSheet
maxRow = shtHui.Cells(Rows.Count, "A").End(xlUp).Row
If maxRow > 1 Then shtHui.Rows("2:" & maxRow).ClearContents
For Each sht In Sheets
strExName = sht.Name
If strExName <> shtHui.Name Then
maxRow_1 = sht.Cells(Rows.Count, "A").End(xlUp).Row
sht.Select
sht.Rows("2:" & maxRow_1).Select
Selection.Copy
maxRow = shtHui.Cells(Rows.Count, "A").End(xlUp).Row
shtHui.Select
Cells(maxRow + 1, "A").Select
Selection.PasteSpecial Paste:=xPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next
shtHui.Select
Cells(2, "B").Select
MsgBox "汇总到表里成功! ", vbExclamation
End Sub
|
|