Working with a combined workbook macro and need to add filename from each source book to Column 1

ShadowSaxx

Board Regular
Joined
Nov 1, 2012
Messages
68
Excel Ver. 2010

I have built a WB that combines the data from 50 or so WBs with a list of the files in Tab "List" and the data from each WB into Tab "Data"

I need to import the file name and write it into Column A. The only code I see is how to fill data that is already in the source sheet such in the code below. My file name is not in the data sheets I am combining so the code below does not work for that.

I am using a list of the files from the Tab "List"

Code from Live Lessons: Excel VBA and Macros with Mr Excel:

' Replicate the department from A2 to column A in WSD
WSD.Cells(NextRow, 1).Resize(RowCount, 1).Value = WSN.Range("A2")


Sub CombineFiles()
Dim WBO As Workbook ' original workbook
Dim WBN As Workbook ' individual data workbooks
Dim WSL As Worksheet ' List of files worksheet
Dim WSD As Worksheet ' data collection worksheet
Dim WSN As Worksheet

' Define object variables
Set WBO = ThisWorkbook
Set WSL = WBO.Worksheets("List")
Set WSD = WBO.Worksheets("Data")

Application.ScreenUpdating = False

' Clear out any previous data on WSD, but leave the headings
WSD.Cells(2, 1).Resize(Rows.Count - 1, Columns.Count).Clear

NextRow = 2

' Loop through all the files on WSL
FinalRow = WSL.Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To FinalRow
ThisFile = WSL.Cells(i, 1)
' Open a file
Set WBN = Workbooks.Open(Filename:=ThisFile)

For Each WSN In WBN.Worksheets

' Last row in this file? RowCount is 4 less <- Not Used Here use -1 to not grab headers.
LastRow = WSN.Cells(Rows.Count, 1).End(xlUp).Row
RowCount = LastRow - 1

' Copy from 5 to last row over to column B
WSN.Cells(2, 1).Resize(RowCount, 20).Copy Destination:=WSD.Cells(NextRow, 1)

-> ' Replicate the department from A2 to column A in WSD
-> ' WSD.Cells(NextRow, 1).Resize(RowCount, 1).Value = WSN.Range("A2")


' Set up the new NextRow
NextRow = NextRow + RowCount
Next WSN

' Close WSN, don't save
WBN.Close SaveChanges:=False

Next i

MsgBox "FILES HAVE BEEN PROCESSED"
Application.ScreenUpdating = True
End Sub

Thanks!
-SS
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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