View Single Post
 
Old 11-12-2024, 07:29 PM
Logit Logit is offline Windows 10 Office 2007
Expert
 
Join Date: Jan 2017
Posts: 591
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

I finally located a solution. New macro with changes :

Code:
Sub CopyDataFromWorkbooks6()
    Dim wb As Workbook
    Dim masterWb As Workbook
    Dim sourceWb As Workbook
    Dim sourceSheet As Worksheet
    Dim masterSheet As Worksheet
    Dim folderPath As String
    Dim fileName As String
    Dim lastRow As Long

    ' Set the folder path
    folderPath = ThisWorkbook.Path & "\" ' Ensure the path ends with a backslash
    fileName = Dir(folderPath & "*.xls*")
    
    ' Create or set the Master Workbook
    Set masterWb = ThisWorkbook
    Set masterSheet = masterWb.Sheets("Sheet1")
    
    
    ' Disable alerts
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    ' Loop through each workbook in the folder
    Do While fileName <> ""
    
        Set sourceWb = Workbooks.Open(folderPath & fileName, ReadOnly:=False)
        On Error Resume Next
        Set sourceSheet = sourceWb.Sheets("Out")
        On Error GoTo 0
        
        If Not sourceSheet Is Nothing Then
         
            ' Find the last row in Master Sheet
            lastRow = masterSheet.Cells(masterSheet.Rows.Count, 1).End(xlUp).Row
            
            ' Copy UsedRange from source sheet and paste to Master Sheet
            sourceSheet.UsedRange.Copy
            masterSheet.Cells(lastRow + 2, 1).PasteSpecial Paste:=xlPasteAll
            
            ' Close the source workbook
            sourceWb.Close False
            masterWb.Save
         
        End If
        
        
        fileName = Dir

        
    Loop
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

ThisWorkbook.Save
    
End Sub
Reply With Quote