房天下 > 房天下问答 > 业主生活 > 其他
  • EXCEL 多文件合并

    有两个问题,第一个,我有若干个结构相同的excel表,(字段相同)我想把他们的指定工作表(sheet)一起输出为一个新excel文件对应sheet的名字就是原文件名第二个,全工作簿增添一列,该列所有值为sheet名

    提问者:猫爱上鼠标

    发布于2011-01-12

共1个回答
  • 间谍王 丨Lv 2
    可以写宏来做,以下是一段代码,用来合并文件Sub 复制工作表() 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 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) intShtCount = intShtCount + 1 '重新命名 strShtName = Replace(strFileName, ".xls", "_") & strShtName 'change by Tony strShtName = "Sheet" & intShtCount ThisWorkbook.Sheets(intShtCount).Name = strShtName ThisWorkbook.Sheets("目录").Cells(i + 1, 1) = strShtName End If Next shtSheet End If Next i Else MsgBox "没有找到符合指定文件,请修改参数后重新搜索!", , "笔锋侠实用工具系列-批量修改文件名 V1.0" End If End With ThisWorkbook.Sheets("目录").Select Application.ScreenUpdating = TrueEnd Sub点击工具->宏->宏 取个名字点创建,复制以上代码,然后把要合并的excel文件和本文件放在一个文件夹,之后运行这个宏,就可以了.
    +10 2011-01-12 举报
热门人气推荐
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。