Return Cell value for all files in a folder (including sub folders)

scotsrule08

New Member
Joined
Jun 21, 2018
Messages
45
I am needing to retrieve the account number from every excel document in a folder including sub folders (roughly 3000 excel files) . The account number is in the same cell (G23) on all excel workbooks.

Ideally the macro would return cell value in column A and in column B have the file location.


Example

A: B:
123123123 :/Users/MyDocuments/Completed
124121241 :/Users/MyDocuments/NeedsToBeWorked


TIA for any help
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try this code. This should get you started.

Sub Extract_Data()

'First Dimention "x" = to Workbook and "y" = to Workbook.
'Second Dimention "ws1" = Worksheet and "ws2" = to Worksheet.
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet

'"x" is a path where data is copied.
'"y" is a path where to paste copied data.
Set x = Workbooks.Open("C:\124121241 :/Users/MyDocuments/NeedsToBeWorked")
Set y = Workbooks.Open("C:\123123123 :/Users/MyDocuments/Completed ")

'First set is determining what Sheet to copy data from. Notice that ws1= to "x" which is a path where data is copied from.
'Second set is determining what Sheet to paste copied data. Notice that ws2= to "y" which is path where data is pasting.
Set ws1 = x.Sheets("Sheet1")
Set ws2 = y.Sheets("Sheet1")

'This is stating what range to copy and from which Worksheet follow by what Worksheet to paste.
'Note that paste range is specified and needs to be changed for every new Worksheet that is being copied.
ws1.Cells.Range("A1:R6").Copy ws2.Range("A1")

'This is to close every Workbook that is open
y.Close True
x.Close False

Hope this helps.
 
Upvote 0
I really was hoping to accomplish this task without having to open and close every workbook, but I understand if it’s not feasible.

Try this code. This should get you started.

Sub Extract_Data()

'First Dimention "x" = to Workbook and "y" = to Workbook.
'Second Dimention "ws1" = Worksheet and "ws2" = to Worksheet.
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet

'"x" is a path where data is copied.
'"y" is a path where to paste copied data.
Set x = Workbooks.Open("C:\124121241 :/Users/MyDocuments/NeedsToBeWorked")
Set y = Workbooks.Open("C:\123123123 :/Users/MyDocuments/Completed ")

'First set is determining what Sheet to copy data from. Notice that ws1= to "x" which is a path where data is copied from.
'Second set is determining what Sheet to paste copied data. Notice that ws2= to "y" which is path where data is pasting.
Set ws1 = x.Sheets("Sheet1")
Set ws2 = y.Sheets("Sheet1")

'This is stating what range to copy and from which Worksheet follow by what Worksheet to paste.
'Note that paste range is specified and needs to be changed for every new Worksheet that is being copied.
ws1.Cells.Range("A1:R6").Copy ws2.Range("A1")

'This is to close every Workbook that is open
y.Close True
x.Close False

Hope this helps.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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