Extracting data from all files in the same folder

Corleone

Well-known Member
Joined
Feb 2, 2003
Messages
841
Office Version
  1. 365
Hi
Im trying to put together some code that will open up all of the other files in the same folder as the one im in
and then select all the data from cell A7 down to wherever the data finishes in column H in all of the files one by one and paste it into my blank spreadsheet from which the macro will be running from.


Im currently getting the following message "File not found" on the following line
SourceFile = Dir(SourcePath & "*.xlsm")

I can confirm that all of the file extensions in the directory are .xlsm

Below is the code

Sub CopyDataFromOtherFiles()
Dim SourcePath As String
Dim SourceFile As String
Dim SourceWorkbook As Workbook
Dim SourceSheet As Worksheet
Dim LastRow As Long
Dim TargetRow As Long
Dim TargetSheet As Worksheet

' Set the source path to the current directory
SourcePath = ThisWorkbook.Path & "C:\Users\shegarty\OneDrive - Network Rail\Profile\Desktop\Latest Analytical Tools - Copy\Period 05 (P04 Contractor Programmes)"

' Set the target worksheet in your current workbook
Set TargetSheet = ThisWorkbook.Sheets(2)

' Initialize the target row to the next available row after existing data
TargetRow = TargetSheet.Cells(TargetSheet.Rows.Count, "A").End(xlUp).Row + 1

' Loop through all files in the source directory
SourceFile = Dir(SourcePath & "*.xlsm")

Do While SourceFile <> ""
If SourceFile <> ThisWorkbook.Name Then ' Exclude the current workbook
' Open the source workbook
Set SourceWorkbook = Workbooks.Open(SourcePath & SourceFile)

' Set the source sheet (tab)
Set SourceSheet = SourceWorkbook.Sheets(2)

' Find the last row with data in columns A to H on the source sheet
LastRow = SourceSheet.Cells(SourceSheet.Rows.Count, "A").End(xlUp).Row

' Copy data from source to target
SourceSheet.Range("A2:H" & LastRow).Copy TargetSheet.Cells(TargetRow, 1)

' Close the source workbook without saving changes
SourceWorkbook.Close False

' Update the target row for the next file
TargetRow = TargetRow + (LastRow - 1)
End If

' Move to the next file
SourceFile = Dir
Loop
End Sub




thanks
 
Apologies - im having a mare - i have now copied in that slash now

This is the directory that is coming up - I manually typed it out not realising i could do a screen shot
1691151628114.png


These are the files in the folder as you can see they are all .xlsm
im just doing it in a test area at the moment - when its set up properly there will be around 30 different files
1691151785099.png


Im logging off for a bit now -thanks for all your help
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Are you getting to that folder using the URL being returned in the thread, or are you manually browsing to it?

So, if you open up a browser window, and put the following in the URL line at the top:
Rich (BB code):
https://networkrail-my.sharepoint.com/personal/shegarty_networkrail_co_uk/Documents/Profile/Desktop/Latest Analytical Tools - Copy/ Period 05 (P04 Contractor Programmes)/
does it open the folder and show all those files?

If not, then you may not be able to navigate to the folder that way.

If you can see the files when you do it that way, I have no idea why it would not be working.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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