【问题标题】:Loop through files in folder and copy/paste to master file遍历文件夹中的文件并复制/粘贴到主文件
【发布时间】:2021-07-04 18:27:42
【问题描述】:

我在一个文件夹中有 3 个文件和一个主模板。

我想:

  1. 遍历这些文件,然后将内容复制到主文件。
  2. 每个 WHOLE 文件都将粘贴到主文件中的新工作表中。
  3. 新工作表的名称将与文件名相同。

以下代码不起作用,缺少功能 2 和 3。

Sub AllFiles()
Application.EnableCancelKey = xlDisabled
Dim folderPath As String
Dim Filename As String
Dim wb As Workbook
Dim sh As Worksheet
folderPath = "C:\Users\Ryan\Desktop\LoopThroughFolders\Sample1\" 'contains folder path
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
Filename = Dir(folderPath & "*.xlsx")
Do While Filename <> ""
    Application.ScreenUpdating = False
    
    Set wb = Workbooks.Open(folderPath & Filename)
    
    Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Copy

    'Not working well here as it will be overwritten by the next file 
    Workbooks("Master Template").Worksheets("Sheet1").Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
    
    Workbooks(Filename).Close
    Filename = Dir
Loop
   Application.ScreenUpdating = True
End sub

【问题讨论】:

标签: vba excel excel-2010


【解决方案1】:

试试下面的代码(解释在代码cmets里面):

Option Explicit

Sub AllFiles()

Application.EnableCancelKey = xlDisabled

Dim folderPath As String
Dim Filename As String
Dim wb As Workbook
Dim Masterwb  As Workbook
Dim sh As Worksheet
Dim NewSht As Worksheet
Dim FindRng As Range
Dim PasteRow As Long

' set master workbook
Set Masterwb = Workbooks("Master Template.xlsx")

folderPath = "C:\Users\Ryan\Desktop\LoopThroughFolders\Sample1\" 'contains folder path

If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
Application.ScreenUpdating = False

Filename = Dir(folderPath & "*.xls*")
Do While Filename <> ""
    Set wb = Workbooks.Open(folderPath & Filename)

    If Len(wb.Name) > 35 Then
        MsgBox "Sheet's name can be up to 31 characters long, shorten the Excel file name"
        wb.Close False
        GoTo Exit_Loop
    Else
        ' add a new sheet with the file's name (remove the extension)
        Set NewSht = Masterwb.Worksheets.Add(After:=Masterwb.Worksheets(1))
        NewSht.Name = Replace(wb.Name, ".xlsx", "")
    End If

    ' loop through all sheets in opened wb
    For Each sh In wb.Worksheets
        ' get the first empty row in the new sheet
        Set FindRng = NewSht.Cells.Find(What:="*", Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)

        If Not FindRng Is Nothing Then ' If find is successful
            PasteRow = FindRng.Row + 1
        Else ' find was unsuccessfull > new empty sheet, should paste at the first row
            PasteRow = 1
        End If

        sh.UsedRange.Copy
        NewSht.Range("A" & PasteRow).PasteSpecial xlPasteValues
    Next sh
    wb.Close False

Exit_Loop:
    Set wb = Nothing
    Filename = Dir
Loop

Application.ScreenUpdating = True

End Sub

【讨论】:

  • 如果文件夹中的文件包含 .xlsm(Excel vba 工作簿)并且文件有多个工作表,我应该添加哪些代码?提前致谢!
  • @Ryan 尝试编辑代码,应该适用于 "xlsx""xlsm"
  • 谢谢!这真的很有帮助!
猜你喜欢
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 2018-09-04
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2020-12-07
相关资源
最近更新 更多