本文共 3370 字,大约阅读时间需要 11 分钟。
Sub MergeAllWorkbooks() Dim SummarySheet As Worksheet Dim FolderPath As String Dim NRow As Long Dim FileName As String Dim WorkBk As Workbook Dim SourceRange As Range Dim DestRange As Range ' Create a new workbook and set a variable to the first sheet. Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1) ' Modify this folder path to point to the files you want to use. FolderPath = "C:\Users\Peter\invoices\" ' NRow keeps track of where to insert new rows in the destination workbook. NRow = 1 ' Call Dir the first time, pointing it to all Excel files in the folder path. FileName = Dir(FolderPath & "*.xl*") ' Loop until Dir returns an empty string. Do While FileName <> "" ' Open a workbook in the folder Set WorkBk = Workbooks.Open(FolderPath & FileName) ' Set the cell in column A to be the file name. SummarySheet.Range("A" & NRow).Value = FileName ' Set the source range to be A9 through C9. ' Modify this range for your workbooks. ' It can span multiple rows. Set SourceRange = WorkBk.Worksheets(1).Range("A9:C9") ' Set the destination range to start at column B and ' be the same size as the source range. Set DestRange = SummarySheet.Range("B" & NRow) Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _ SourceRange.Columns.Count) ' Copy over the values from the source to the destination. DestRange.Value = SourceRange.Value ' Increase NRow so that we know where to copy data next. NRow = NRow + DestRange.Rows.Count ' Close the source workbook without saving changes. WorkBk.Close savechanges:=False ' Use Dir to get the next file name. FileName = Dir() Loop ' Call AutoFit on the destination sheet so that all ' data is readable. SummarySheet.Columns.AutoFitEnd Sub
Sub ConslidateWorkbooks()'Created by Sumit Bansal from http://trumpexcel.comDim FolderPath As StringDim Filename As StringDim Sheet As WorksheetApplication.ScreenUpdating = FalseFolderPath = Environ("userprofile") & "\Desktop\Test\"Filename = Dir(FolderPath & "*.xls*")Do While Filename <> "" Workbooks.Open Filename:=FolderPath & Filename, ReadOnly:=True For Each Sheet In ActiveWorkbook.Sheets Sheet.Copy After:=ThisWorkbook.Sheets(1) Next Sheet Workbooks(Filename).Close Filename = Dir()LoopApplication.ScreenUpdating = TrueEnd Sub
Sub 合并当前目录下所有工作簿的全部工作表()Dim mypath, myname, awbnameDim wb As Workbook, wbn As StringDim g As LongDim num As LongDim box As StringApplication.ScreenUpdating = Falsemypath = ActiveWorkbook.Pathmyname = Dir(mypath & "\" & "*.xls")awbname = ActiveWorkbook.Namenum = 0Do While myname <> ""If myname <> awbname ThenSet wb = Workbooks.Open(mypath & "\" & myname)num = num + 1With Workbooks(1).ActiveSheet.Cells(.Range("a65536").End(xlUp).Row + 2, 1) = Left(myname, Len(myname) - 4)For g = 1 To Sheets.Countwb.Sheets(g).UsedRange.Copy .Cells(.Range("a65536").End(xlUp).Row + 1, 1)Nextwbn = wbn & Chr(13) & wb.Namewb.Close FalseEnd WithEnd Ifmyname = DirLoopRange("a1").SelectApplication.ScreenUpdating = TrueMsgBox "共合并了" & num & "个工作薄下的全部工作表。如下:" & Chr(13) & wbn, vbInformation, "提示"End Sub
转载地址:http://miuml.baihongyu.com/