Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 4812|回复: 4

[已解决]合并文件夹里面很多工作薄到一个工作薄中

[复制链接]
发表于 2011-4-12 11:21 | 显示全部楼层 |阅读模式
我想把一个文件夹里面的很多工作薄合并到一个工作薄里面来,比如:第一个工作薄里面有3张表格,第二个工作薄里面有3张,。。。一直到第10个工作薄里面有3个,那么合并到一个工作薄里面来就有30张工作表。望高手解决 谢谢
最佳答案
2011-4-12 11:43
这不就是和哥一样的问题嘛,吼吼
Sub ImportData()
    Dim MyObject As Object
    Dim strPath As String, strFileName As String, strMyName As String
    Dim shtSheet As Worksheet, strShtName As String
    Dim intCount As Integer, intShtCount As Integer, i As Integer
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    strPath = ThisWorkbook.Path
    strMyName = ThisWorkbook.Name
    intShtCount = ThisWorkbook.Sheets.Count
    With Application.FileSearch
        .NewSearch
        .LookIn = strPath
        .SearchSubFolders = False
        .Filename = ".xls"
        .FileType = msoFileTypeOfficeFiles
        If .Execute() > 0 Then
            intCount = .FoundFiles.Count
            For i = 1 To intCount
                strFileName = Replace(.FoundFiles(i), strPath & "\", "")
                If strFileName <> strMyName Then
                    Set MyObject = GetObject(strPath & "/" & strFileName)
                    For Each shtSheet In MyObject.Worksheets
                        strShtName = shtSheet.Name
                        If MyObject.Sheets(strShtName).UsedRange.Count > 1 Then
                            MyObject.Sheets(strShtName).Copy After:=ThisWorkbook.Sheets(intShtCount)
                        End If
                    Next shtSheet
                    MyObject.Close
                End If
            Next i
        Else
            MsgBox "Invalid option!", vbCritical, "Wrong data"
    End If
    End With
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    MsgBox "Data Imported", vbInformation, "Confirmation"
End Sub

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2011-4-12 11:35 | 显示全部楼层
回复

使用道具 举报

发表于 2011-4-12 11:43 | 显示全部楼层    本楼为最佳答案   
这不就是和哥一样的问题嘛,吼吼
Sub ImportData()
    Dim MyObject As Object
    Dim strPath As String, strFileName As String, strMyName As String
    Dim shtSheet As Worksheet, strShtName As String
    Dim intCount As Integer, intShtCount As Integer, i As Integer
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    strPath = ThisWorkbook.Path
    strMyName = ThisWorkbook.Name
    intShtCount = ThisWorkbook.Sheets.Count
    With Application.FileSearch
        .NewSearch
        .LookIn = strPath
        .SearchSubFolders = False
        .Filename = ".xls"
        .FileType = msoFileTypeOfficeFiles
        If .Execute() > 0 Then
            intCount = .FoundFiles.Count
            For i = 1 To intCount
                strFileName = Replace(.FoundFiles(i), strPath & "\", "")
                If strFileName <> strMyName Then
                    Set MyObject = GetObject(strPath & "/" & strFileName)
                    For Each shtSheet In MyObject.Worksheets
                        strShtName = shtSheet.Name
                        If MyObject.Sheets(strShtName).UsedRange.Count > 1 Then
                            MyObject.Sheets(strShtName).Copy After:=ThisWorkbook.Sheets(intShtCount)
                        End If
                    Next shtSheet
                    MyObject.Close
                End If
            Next i
        Else
            MsgBox "Invalid option!", vbCritical, "Wrong data"
    End If
    End With
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    MsgBox "Data Imported", vbInformation, "Confirmation"
End Sub

回复

使用道具 举报

发表于 2012-4-22 17:59 | 显示全部楼层
Kratos 发表于 2011-4-12 11:43
这不就是和哥一样的问题嘛,吼吼
Sub ImportData()
    Dim MyObject As Object

楼主好像不能运行, With Application.FileSearch
提示错误
回复

使用道具 举报

发表于 2013-3-7 17:06 | 显示全部楼层
对象不支持该动作
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-4 08:51 , Processed in 0.282704 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表