zoukankan      html  css  js  c++  java
  • 合并多个工作薄workbooks到一个工作薄workbook

    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:UsersPeterinvoices"
        
        ' 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.AutoFit
    End Sub
    

    将多个工作薄所有 sheet 放到同一个工作薄

    Sub ConslidateWorkbooks()
    'Created by Sumit Bansal from http://trumpexcel.com
    Dim FolderPath As String
    Dim Filename As String
    Dim Sheet As Worksheet
    Application.ScreenUpdating = False
    FolderPath = Environ("userprofile") & "DesktopTest"
    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()
    Loop
    Application.ScreenUpdating = True
    End Sub
    

    将多个工作薄所有 sheet 放到同一个工作薄sheet中

    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 = ActiveWorkbook.Name
    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.Name
    wb.Close False
    End With
    End If
    myname = Dir
    Loop
    Range("a1").Select
    Application.ScreenUpdating = True
    MsgBox "共合并了" & num & "个工作薄下的全部工作表。如下:" & Chr(13) & wbn, vbInformation, "提示"
    End Sub
    
  • 相关阅读:
    jQuery
    jQuery
    jQuery
    jQuery
    jQuery 遍历- 过滤:缩小搜索元素的范围
    jQuery 遍历
    jQuery 遍历
    jQuery 遍历
    jQuery 遍历:jQuery 遍历 什么是遍历?
    jQuery 尺寸:处理元素和浏览器窗口的尺寸
  • 原文地址:https://www.cnblogs.com/flowerszhong/p/6232211.html
Copyright © 2011-2022 走看看