VBA copying data from all files in a folder

bhbhbh

New Member
Joined
Oct 12, 2018
Messages
1
Hi everyone,

I have written a small piece of code but I am having trouble finishing it off.

Below my code opens all the files in a folder, selects a sheet and copies a range of cells which it then pastes to a master spreadsheet.
After I have pasted the values to the master spreadsheet I need to return to the file (from where I copied the data), close it (don’t have to save this one) and then move on to the next file i.e. Worksheets (q)

The code is fine for the first sheet, but then I get an error when It comes to the next file. I hope you are able to help me out with this. It would be a lifesaver.

I am using excel 2016 on a windows 7 machine.



Sub LoopThroughDirectorytoEdit()
Dim MyFile As String
Dim Filepath As String
Dim q As Long

Filepath = "NAME"
MyFile = Dir(Filepath)

Do While Len(MyFile) > 0

If MyFile = "bookz.xlsm" Then
Exit Sub
End If
Workbooks.Open (Filepath & MyFile)
For q = 1 To Application.Worksheets.Count
Worksheets(q).Activate

Sheets("Log").Select
Range("A2:F1000").Select
Selection.Copy
Windows("MASTER SPREADSHEET NAME").Activate
Sheets("AllActivity").Select
'Finds empty row'
NextRow = Sheets("AllActivity").Range("A" & Rows.Count).End(xlUp).Row + 1
ActiveSheet.Paste


Next q
ActiveWorkbook.Save
ActiveWorkbook.Close
MyFile = Dir
Loop
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,215,309
Messages
6,124,180
Members
449,146
Latest member
el_gazar

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