合并多个工作薄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:\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.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") & "\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()
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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VBA是Visual Basic for Applications(VBA)的缩写,它是一种程序语言,可用于在微软的Office应用程序中自动化任务。当我们需要合并多个工作下的指定工作表的指定列时,可以使用VBA来完成这个任务。 首先,我们需要打开一个新的工作,作为合并结果的目标工作。然后,我们需要编写VBA代码来遍历每个源工作,并复制指定的工作表和列到目标工作。 下面是一个示例代码,它假设要合并工作的名称已知,合并工作表为Sheet1,需要合并的列为A列: ``` Sub 合并工作() Dim 目标工作 As Workbook Dim 源工作 As Workbook Dim 目标工作表 As Worksheet Dim 源工作表 As Worksheet Dim 目标列 As Range Dim 源列 As Range Set 目标工作 = ThisWorkbook '将当前工作设为目标工作 Set 目标工作表 = 目标工作.Sheets("Sheet1") '设置目标工作表 '遍历源工作 For Each 源工作 In Workbooks If 源工作.Name <> 目标工作.Name Then '跳过目标工作 Set 源工作表 = 源工作.Sheets("Sheet1") '设置源工作表 Set 目标列 = 目标工作表.Range("A1:A" & 源工作表.Cells(Rows.Count, 1).End(xlUp).Row) '设置目标列 Set 源列 = 源工作表.Range("A1:A" & 源工作表.Cells(Rows.Count, 1).End(xlUp).Row) '设置源列 源列.Copy 目标列 '复制源列到目标列 End If Next 源工作 MsgBox "合并完成!" End Sub ``` 在上面的代码中,我们使用了一些变量来引用目标工作、源工作、目标工作表、源工作表、目标列和源列。我们使用循环遍历源工作,并使用Copy方法将源列中的数据复制到目标列中。最后,我们弹出一个对话框来显示合并完成的消息。 可以根据实际需求对上面的代码进行修改,比如指定不同的工作表和列。希望这个回答对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值