Extract Specific cell data from Worksbooks in all subfolders

Orfevre

New Member
Joined
Jul 11, 2022
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hello, I need some help getting specific cell information into my report worksheet from all excel files in the folder and all subfolders. I have managed to get the code to open all the files in the directory but whenever I try to extract the cell information I get an error.

I am wanting it to run through each file, bring in information from multiple cells in all the files to create a report.

VBA Code:
Sub copydata()
    Call GetFiles("D:\data\Rev\Analysis\records\files\")
End Sub

Sub GetFiles(ByVal path As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

Dim folder As Object
Set folder = fso.GetFolder(path)

Dim subfolder As Object
Dim file As Object

For Each subfolder In folder.SubFolders
    GetFiles (subfolder.path)
Next subfolder

Application.ScreenUpdating = False

For Each file In folder.Files
    Workbooks.Open (file)
    Range("A2").Offset(r).Value = .Range("C5").Value
    Range("B2").Offset(r).Value = .Range("D51").Value
    Workbooks.Close (file)
Next file

Set fso = Nothing
Set folder = Nothing
Set subfolder = Nothing
Set file = Nothing

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Welcome to the Board!

I see one thing that would cause an error. Specifically, these two lines:
Rich (BB code):
    Range("A2").Offset(r).Value = .Range("C5").Value
    Range("B2").Offset(r).Value = .Range("D51").Value

Note how you have the two parts bolded in red start with a period.
You can only do that if those lines are contained within a WITH block (because the periods tells it that it belongs to an object (sheet) specified in the WITH portion, i.e.)
VBA Code:
With Sheets("Sheet1")
    Range("A2").Offset(r).Value = .Range("C5").Value
    Range("B2").Offset(r).Value = .Range("D51").Value
End With

To try to use bits of code that start with a period without using WITH will cause errors, because Excel cannot tell which object they are supposed to belong to.

See here for an explanation: VBA With Statement (With - End With)

When dealing with multiple files, you should probably qualify where all the ranges are coming from (what file and sheet they belong to).
 
Upvote 0
Welcome to the Board!

I see one thing that would cause an error. Specifically, these two lines:
Rich (BB code):
    Range("A2").Offset(r).Value = .Range("C5").Value
    Range("B2").Offset(r).Value = .Range("D51").Value

Note how you have the two parts bolded in red start with a period.
You can only do that if those lines are contained within a WITH block (because the periods tells it that it belongs to an object (sheet) specified in the WITH portion, i.e.)
VBA Code:
With Sheets("Sheet1")
    Range("A2").Offset(r).Value = .Range("C5").Value
    Range("B2").Offset(r).Value = .Range("D51").Value
End With

To try to use bits of code that start with a period without using WITH will cause errors, because Excel cannot tell which object they are supposed to belong to.

See here for an explanation: VBA With Statement (With - End With)

When dealing with multiple files, you should probably qualify where all the ranges are coming from (what file and sheet they belong to).
Thank you I have amended per your suggestion which now runs, however no data is copied from the files I cannot understand why if it runs without error.

VBA Code:
Sub Copdata()
Call GetFiles("D:\data\Rev\Analysis\records\files\")
End Sub

Sub GetFiles(ByVal path As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

Dim folder As Object
Set folder = fso.GetFolder(path)

Dim subfolder As Object
Dim file As Object

For Each subfolder In folder.SubFolders
    GetFiles (subfolder.path)
Next subfolder

Application.ScreenUpdating = False

For Each file In folder.Files
    Set fromWorkbook = Workbooks.Open(file)
    With fromWorkbook.Worksheets("Dashboard")
        Range("A2").Offset(r).Value = .Range("C5").Value
        Range("B2").Offset(r).Value = .Range("D51").Value
    End With
    fromWorkbook.Close savechanges:=False
Next file

Set fso = Nothing
Set folder = Nothing
Set subfolder = Nothing
Set file = Nothing

End Sub
 
Upvote 0
Your code seems to be missing a few things?
Where exactly are you trying to copy the data to?
The file with the VBA code/macros in it?

If so, you need to qualify that workbook too.
Otherwise, theses parts of your code (in red):
Rich (BB code):
        Range("A2").Offset(r).Value = .Range("C5").Value
        Range("B2").Offset(r).Value = .Range("D51").Value
will just default to whatever the active workbook is at the time (and if you just open a file, that last opened file will be the active workbook).

Also, you have not set the "r" in your Offset command to anything.
 
Upvote 0
Solution
Your code seems to be missing a few things?
Where exactly are you trying to copy the data to?
The file with the VBA code/macros in it?

If so, you need to qualify that workbook too.
Otherwise, theses parts of your code (in red):
Rich (BB code):
        Range("A2").Offset(r).Value = .Range("C5").Value
        Range("B2").Offset(r).Value = .Range("D51").Value
will just default to whatever the active workbook is at the time (and if you just open a file, that last opened file will be the active workbook).

Also, you have not set the "r" in your Offset command to anything.
Thank for you help and easy to understand explanations
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0
Hello, I need some help getting specific cell information into my report worksheet from all excel files in the folder and all subfolders. I have managed to get the code to open all the files in the directory but whenever I try to extract the cell information I get an error.

I am wanting it to run through each file, bring in information from multiple cells in all the files to create a report.

VBA Code:
Sub copydata()
    Call GetFiles("D:\data\Rev\Analysis\records\files\")
End Sub

Sub GetFiles(ByVal path As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

Dim folder As Object
Set folder = fso.GetFolder(path)

Dim subfolder As Object
Dim file As Object

For Each subfolder In folder.SubFolders
    GetFiles (subfolder.path)
Next subfolder

Application.ScreenUpdating = False

For Each file In folder.Files
    Workbooks.Open (file)
    Range("A2").Offset(r).Value = .Range("C5").Value
    Range("B2").Offset(r).Value = .Range("D51").Value
    Workbooks.Close (file)
Next file

Set fso = Nothing
Set folder = Nothing
Set subfolder = Nothing
Set file = Nothing

End Sub
You have to do nothing.
No Vba required

Just follow the below some easy steps.

1. Make sure that your files have same table name (from where you want to copy the data)
2. Open you Master excel file where you want to get/copy the data
3. Click "Data"
4. Click "Get Data"
5. Select "From Folder"
6. Select your main Folder and click Open (this main folder may have contained many subfolders and files)
7. Excel will display a list of all files.
8. Click "Combine" and select "Combine & Load To"
9. Select Sample File "First File"
10. Click "Petameters"
11. Select the Table name which you want to import. On the right side, you can view the table data.
12. Click "OK"
13. Select "Table" option, and change nothing and click "Ok"
14. Excel will load the data from all files.
15. This data is linked to your selected folder, whenever you create a new file or edit any file, data will be updated in your Master File.

Thank you
 
Upvote 0

Forum statistics

Threads
1,215,519
Messages
6,125,294
Members
449,218
Latest member
Excel Master

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