VBA 工作簿合并

本文介绍了如何使用VBA来批量合并多个Excel工作簿,包括从选定文件和整个文件夹中进行汇总,采用递归方式遍历文件夹内的所有文件。
摘要由CSDN通过智能技术生成

窗体设计如下i:

 1.1、工作簿汇总(选择文件:可以选择多个文件

Sub combineworkbooks()

Dim fd As FileDialog
Dim vsi As Variant
Dim wb As Workbook
Dim sht As Worksheet
Dim tm As Variant
Dim dt As Variant
Dim n As Variant
Dim wbName As Variant
Dim wbSheetCount As Variant
Dim newSheetFullName As Variant
Dim newSheetName As Variant

Application.DisplayAlerts = False

'tm = Hour(Time) & Minute(Time) & Second(Time)
tm = Format(Time, "hhmmss")
dt = Format(Date, "yyyymmdd")


Set fd = Application.FileDialog(msoFileDialogFilePicker)      '允许用户选择文件
With fd
    .Filters.Clear                                            '清除现有文件类型
    .Title = "选择需要合并的工作簿"
    .Filters.Add "Excel文件", "*.xls;*.xlsx"                  '添加文件格式
    .AllowMultiSelect = True                                  '允许选择多个文件

    If .Show = -1 Then                                        '显示窗体,且选择文件

starttime = Timer
        
        Set wb = Workbooks.Add
        Set sht = wb.Worksheets(1)
        sht.Name = "文件目录"
        sht.Range("A1:B1") = Array("文件名", "地址(超链接)")             '表头
        wb.SaveAs ThisWorkbook.path & "\文件汇总表" & "(" & dt & "-" & tm & ")" & ".xlsx"
        wbName = "文件汇总表" & "(" & dt & "-" & tm & ")" & ".xlsx"

        n = 2
        For Each vsi In .SelectedItems                                                  '遍历选择的文件
'            sht.Range("b"
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值