metalscuba
New Member
- Joined
- Jul 27, 2024
- Messages
- 2
- Office Version
- 365
- Platform
- 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.
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.
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**