Adding a column with workbook name in multiple workbooks

fawnlemur

New Member
Joined
Dec 9, 2018
Messages
29
Hi

I have over 500 workbooks, I want to add the workbook name in column AD for every row with text in it for all 500 spreadsheets.
I plan to merge them into one master spreadsheet but I need to know which spreadsheet the data came from.
Can someone please help me with the first part to add the workbook's name in column AD for every spreadsheet as I'm not too sure how I can do that.


I need the workbook name not the sheet name.

Thank you
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Are all the workbooks in the same folder?
 
Upvote 0
How about
Code:
Sub fawnlemur()
   Dim Pth As String
   Dim Fname As String
   Dim Wbk As Workbook
   Dim Ws As Worksheet
   
   Application.ScreenUpdating = True
   Pth = [COLOR=#ff0000]"C:\MrExcel\Fluff\"[/COLOR]
   Fname = Dir(Pth & "*.xls*")
   Do While Fname <> ""
      Set Wbk = Workbooks.Open(Pth & Fname)
      For Each Ws In Wbk.Worksheets
         Ws.Range("AD1:AD" & Ws.Range("A" & Rows.Count).End(xlUp).Row).Value = Fname
      Next Ws
      Wbk.Close True
      Fname = Dir()
   Loop
End Sub
This uses column A to decide the last row, but than can easily be changed if needed.
Change path in red to suit
 
Last edited:
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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