Code not copying range from all sheet in folder

scassells

New Member
Joined
Aug 8, 2018
Messages
13
Only paste the values from the first workbook and then it appears to not locate the next open row??

Sub Loopthrudirectory()
Dim myfile As String
Dim erow
myfile = Dir("C:\AA_HISTORY")

Do While Len(myfile) > 0
If myfile = "activity Log.xlsm" Then
Exit Sub
End If
Workbooks.Open (myfile)
Range("ak4:AT13").Copy
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 3).End(xlUp).Offset(3, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 2), Cells(erow, 11))
myfile = Dir
Loop

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi welcome to forum

I suspect part of your problem is this part of your code

Code:
If myfile = "activity Log.xlsm" Then
Exit Sub
End If


which will exit the code if the file exists in the specified folder.

Try this update & see if it does what you want

Code:
Sub Loopthrudirectory()
    Dim myfile As String, mypath As String
    Dim Wb As Workbook
    Dim wsDest As Worksheet
    
    mypath = "C:\AA_HISTORY\"
    myfile = Dir(mypath & "*xl*")
    
    Set wsDest = ThisWorkbook.Worksheets("Sheet1")
    
    Application.ScreenUpdating = False
    Do While Len(myfile) > 0
    If myfile <> "activity Log.xlsm" Then
        Set Wb = Workbooks.Open(mypath & myfile, False, True)
        Wb.Sheets(1).Range("AK4:AT13").Copy _
        wsDest.Cells(wsDest.Cells(wsDest.Rows.Count, 3).End(xlUp).Offset(3, 0).Row, 2)
        Wb.Close False
    End If
    
    Set Wb = Nothing
    myfile = Dir
Loop


    Application.ScreenUpdating = True
End Sub

Dave
 
Last edited:
Upvote 0
I have since modified the code and so the following code does successfully loop through all of thesheets in the directory however it does not paste to the next open row in thedestination sheet and instead clobbers the data in the same rows

Sub copydatafrommulttomaster()
Dim folderpath As String, Filepath As String, filename AsString
folderpath = "C:\Users\Stvcass\Documents\AA_HISTORY"
Filepath = folderpath & "*.xls*"
filename = Dir(Filepath)
Do While filename <> ""
If myfile = "activity Log.xlsm" Then
Exit Sub
End If
Workbooks.Open (folderpath & filename)
Application.DisplayAlerts = False
Range("AK4:AT15").Select
Range("AK4:AT15").Copy
'Application.DisplayAlerts = False
Activewookbook.Close
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1,0).Row
ActiveSheet.PasteDestination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow,4))
filename = Dir
Loop
Application.DisplayAlerts = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top