Copy File Names to Destination File

metalscuba

New Member
Joined
Jul 27, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Hope you everyone is well..

I am trying to copy/paste file name to my destination workbook's column A. Below is copying data from all workbooks from a folder and pastes them to my main file.

VBA Code:
Application.ScreenUpdating = False
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Dim LastRow As Long
    Const strPath As String = "folder of data files"
    ChDir strPath
    strExtension = Dir("*.csv*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("ADJUSTMENTS_EXTR").Cells.Find("Total", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Sheets("ADJUSTMENTS_EXTR").Range("A10:V" & LastRow).Copy wkbDest.Sheets("Consolidated Data").Cells(Rows.Count, "B").End(xlUp).Offset(0, 0)
            
            **'.wkbDest.Sheets("Consolidated Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = wkbSource.Name**
            .Close savechanges:=False
        End With
        
        
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True

There are usually 5-6 files under data folder. Aim is to while copying data, I also need each file's name for each row at my main file's column "A" alongside with data. below is what I tried. Unfortunately couldn't make it work. Thanks in advance.

VBA Code:
.wkbDest.Sheets("Consolidated Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = wkbSource.Name**
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
On that line of code get rid of the leading dot. With a leading dot, as the line is inserted in the macro, it will refer to the wbkSource file (With wkbSource) which is incorrect since you, instead, need to reference wkbDest.
 
Upvote 0

Forum statistics

Threads
1,224,802
Messages
6,181,054
Members
453,014
Latest member
Chris258

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