Copy filename into report?

id107

Well-known Member
Joined
Apr 16, 2008
Messages
580
Hi. I have a workbook that compiles data that our team has completed each week. They fill in a tracker (A2:I300), send it back and I save them all into one folder amd run a macro to compile the report. Is there any way to also import the name of each file into this report? Preferably into column J.

The code I am currently using is:
Code:
Sub Compile_Data()
    Dim myDir As String, fn As String
    myDir = "m:\weekly_reporting\"
fn = Dir(myDir & "*.xls")
Do While fn <> ""
    If fn <> ThisWorkbook.Name Then
        With Workbooks.Open(myDir & fn)
            With .Sheets(1).Range("A2:I300")
                ThisWorkbook.Sheets(1).Range("a" & Rows.Count).End(xlUp)(2) _
                .Resize(.Rows.Count, .Columns.Count).Value = .Value
            End With
            .Close False
        End With
    End If
    fn = Dir
Loop
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi id107,

If I'm correct, you have the FileName in fn, so while you still have the file open, copy fn into J1. maybe something like

Code:

If fn <> ThisWorkbook.Name Then
With Workbooks.Open(myDir & fn)
With .Sheets(1).Range("A2:I300")
ThisWorkbook.Sheets(1).Range("a" & Rows.Count).End(xlUp)(2) _
.Resize(.Rows.Count, .Columns.Count).Value = .Value

"ThisWorkbook.Sheets(1).Range("J1") = fn"

End With
.Close False
End With
End If

ColinKJ
 
Upvote 0
That's a very good point with the fn.

I'm trying to get the filename to be listed beside the data from the file and I've been trying to get this applied (your code helped a lot in my attempts), but I can't get it to line up properly.

Apologies. I'm ill and nothing is going well today.
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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