excel合并工作簿和工作表的代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
把多个工作簿合并到一个工作簿作为新工作簿的一张表(宏代码)
Sub 合并当前目录下所有工作簿的全部工作表()
Dim MyPath, MyName, AWbName
Dim Wb As Workbook, WbN As String
Dim G As Long
Dim Num As Long
Dim BOX As String
Application.ScreenUpdating = False
MyPath = ActiveWorkbook.Path
MyName = Dir(MyPath & "\" & "*.xls")
AWbName =
Num = 0
Do While MyName <> ""
If MyName <> AWbName Then
Set Wb = Workbooks.Open(MyPath & "\" & MyName)
Num = Num + 1
With Workbooks(1).ActiveSheet
.Cells(.Range("A65536").End(xlUp).Row + 2, 1) = Left(MyName, Len(MyName) - 4)
For G = 1 To Sheets.Count
Wb.Sheets(G).UsedRange.Copy .Cells(.Range("A65536").End(xlUp).Row + 1, 1)
Next
WbN = WbN & Chr(13) &
Wb.Close False
End With
End If
MyName = Dir
Loop
Range("A1").Select
Application.ScreenUpdating = True
MsgBox "共合并了" & Num & "个工作薄下的全部工作表。如下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub
具体操作:在工作簿目录下新建一工作簿,工具---宏----编辑器----插入—模块---粘贴代码==运行
excel如何将一个工作簿中的多个工作表合并到一张工作表上
打开你的工作簿新建一个工作表在这个工作表的标签上右键查看代码你把下面的代码复制到里边去,然后上面有个运行运行子程序就可以了,代码如下,如果出现问题你可以尝试工具宏宏安全性里把那个降低为中或者低再试试
Sub 合并当前工作簿下的所有工作表()
Application.ScreenUpdating = False
For j = 1 To Sheets.Count
If Sheets(j).Name <> Then
X = Range("A65536").End(xlUp).Row + 1
Sheets(j).UsedRange.Copy Cells(X, 1)
End If
Next
Range("B1").Select
Application.ScreenUpdating = True
MsgBox "当前工作簿下的全部工作表已经合并完毕!", vbInformation, "提示"
End Sub
把同一工作簿多张工作表合并到同一张工作表
1 新建一个工作表放在最左边,ALT + F11 键打开代码框--插入--模块--复制以下代码
ALT + F8 键打开,运行该代码即可
Sub 合并()
For I = 2 To Sheets.Count
'如果工作表的第一行都一样,就把下面Rows("1" & 的1改成2就好了Sheets(I).Rows("1" & ":" & Sheets(I).Range("A60000").End(xlUp).Row). _ Copy Range("A" & Range("A60000").End(xlUp).Row + 1)
Next
End Sub
批量将多个excel中的多个工作簿合并到一个excel中
将要合并的excel放到一个文件夹中,在这个目录中新建一个excel,运行以下代码
Sub CombineFiles()
Dim path As String
Dim FileName As String
Dim LastCell As Range
Dim Wkb As Workbook
Dim WS As Worksheet
Dim ThisWB As String
Dim MyDir As String
MyDir = ThisWorkbook.path & "\"
'ChDrive Left(MyDir, 1) 'find all the excel files
'ChDir MyDir
'Match = Dir$("")
ThisWB =
Application.EnableEvents = False
Application.ScreenUpdating = False
path = MyDir
FileName = Dir(path & "\*.xls", vbNormal)
Do Until FileName = ""
If FileName <> ThisWB Then
Set Wkb = Workbooks.Open(FileName:=path & "\" & FileName)
For Each WS In Wkb.Worksheets
Set LastCell = WS.Cells.SpecialCells(xlCellTypeLastCell)
If LastCell.Value = "" And LastCell.Address = Range("$A$1").Address Then
Else
WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) End If
Next WS
Wkb.Close False
End If
FileName = Dir()
Loop
Application.EnableEvents = True
Application.ScreenUpdating = True
Set Wkb = Nothing
Set LastCell = Nothing
End Sub