Ryan
Ryan

Reputation: 23

Loop through files in folder and copy/paste to master file

I have 3 files in a folder and one master template.

I want to:

  1. Loop through these files then copy the content to a master file.
  2. Each WHOLE file will be pasted to a new worksheet in the master file.
  3. The new worksheet's name will be the same as file's name.

The codes below are not working and missing functions 2 and 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

Upvotes: 2

Views: 27302

Answers (1)

Shai Rado
Shai Rado

Reputation: 33672

Try the code below (explanations are inside the code comments):

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

Upvotes: 2

Related Questions