博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
合并多个工作薄workbooks到一个工作薄workbook
阅读量:7072 次
发布时间:2019-06-28

本文共 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

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

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

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

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/

你可能感兴趣的文章
Android开发之旅:组件生命周期(二)
查看>>
使用LVS+NAT搭建集群实现负载均衡
查看>>
LVM 磁盘分区扩容
查看>>
mysql5.6之key_buffer_size优化设置
查看>>
查看Linux服务器网卡流量小脚本shell和Python各一例
查看>>
Linux TC的ifb原理以及ingress流控
查看>>
AgileEAS.NET之敏捷并行开发方法
查看>>
Java源码分析系列之ArrayList读后感
查看>>
性能测试之手机号码python生成方式
查看>>
统计数据库大小的方法
查看>>
PHP递归遍历文件夹
查看>>
用户系列之五:用户SID查看之终结版
查看>>
ubuntu 11.10下载和编译Android源码
查看>>
千兆级LTE的一小步,5G之路的一大步
查看>>
跟我一起写 Makefile(一)
查看>>
管理日志-原创理论工具--技能方格图
查看>>
MPLS TE第一步:创建基本TE隧道
查看>>
windows中禁止U盘写入
查看>>
Bash技巧总结
查看>>
在窗体中添加标签Label、Icon图标
查看>>