前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ExcelVBA汇总-多簿一表_to_一簿一表

ExcelVBA汇总-多簿一表_to_一簿一表

作者头像
哆哆Excel
发布2023-09-09 10:50:05
2850
发布2023-09-09 10:50:05
举报
文章被收录于专栏:哆哆Excel

ExcelVBA汇总-多簿一表_to_一簿一表

=====start====

1.ExcelVBA汇总多工作簿中指定工作表到新工作簿

2.ExcelVBA汇总多工作簿中指定工作表到新工作簿

3.ExcelVBA一键汇总多文件的指定工作表的到一个文件

=====end====

【问题】

【思路】

1.打开对话框,选择多个文件

2.输入工作表标题行数

3.输入要汇总的工作表包含的字符

4.程序运行

【代码】

代码语言:javascript
复制
    'yhd_2.汇总 -多簿一表_to_一簿一表
Sub yhd_ExcelVBA多簿一表_to_一簿一表()
    Dim title_Row As Integer, ShtNameStr As String, sht_i As Integer, used_Row As Integer, write_row As Integer
    Dim ThisWb As Workbook, OpenWb As Workbook, sht As Worksheet
    t = Timer
    disAppSet (False)
    ChDrive ThisWorkbook.Path
    ChDir ThisWorkbook.Path
    SelectFiles = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", , "打开文件", , True)
    '    Debug.Print TypeName(SelectFiles)
    If TypeName(SelectFiles) = "Boolean" Then MsgBox "你选了“取消”,将退出": Exit Sub
    title_Row = Application.InputBox(prompt:="请输入标题行数:", Type:=1)
    '    Debug.Print TypeName(titleRowS)
    If StrPtr(titleRowS) = 0 Then MsgBox "你选了“取消”,将退出": Exit Sub
    ShtNameStr = Application.InputBox(prompt:="请输入工作表名称:", Type:=2)
    '     Debug.Print TypeName(ShtNameStr)
    If Len(ShtNameStr) = 0 Or StrPtr(ShtNameStr) = 0 Then MsgBox "你选了“取消”,将退出": Exit Sub
    Set ThisWb = ThisWorkbook
    With ThisWb
        If Wsh_Exists("汇总") Then Worksheets("汇总").Delete
        Set Thissht = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
        Thissht.Name = "汇总"
    End With
    sht_i = 1
    For Each FileOne In SelectFiles
        Debug.Print FileOne
        '        ThisSht.Range("A" & i) = FileOne
        Set OpenWb = Workbooks.Open(FileOne)
        With OpenWb
            For Each sht In .Worksheets
                If InStr(sht.Name, ShtNameStr) Then
                    With sht
                        If sht_i = 1 Then
                            .Rows("1:" & title_Row).Copy Thissht.Range("A1")
                        End If
                        used_Row = .UsedRange.Rows.Count
                        write_row = Thissht.UsedRange.Rows.Count + 1
                        .Rows(title_Row + 1 & ":" & used_Row).Copy Thissht.Range("A" & write_row)
                    End With
                    sht_i = sht_i + 1
                End If
            Next
            .Close False
        End With
        Set OpenWb = Nothing
    Next
    MsgBox "合并" & sht_i & "个,用时:" & Format(Timer - t, "0.00秒")
    disAppSet (True)
End Sub
    '用法:disAppSet(true)开disAppSet(true)关
Sub disAppSet(flag As Boolean)
    With Application
        .ScreenUpdating = flag
        .DisplayAlerts = flag
        .AskToUpdateLinks = flag
        If flag Then
            .Calculation = xlCalculationAutomatic
        Else
            .Calculation = xlCalculationManual
        End If
    End With
End Sub
Public Function Wsh_Exists(ByVal sWshName As String) As Boolean
    Dim sName As String
    On Error GoTo ErrorHandler
    sName = ThisWorkbook.Sheets(sWshName).Name
    If Len(sName) > 0 Then Wsh_Exists = True
    Exit Function
ErrorHandler:
    Wsh_Exists = False
End Function

【效果】

25秒完成汇总

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-05-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 哆哆Excel 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档