Hello,
I'm trying to import data from several workbooks into a single one, but I'm trying to print the file name on the first blank column for every row of data. I want to do this step on the original files before importing the data to the new workbook.
The code that I have is able to paste all the information into one workbook, but I still need to create the part of the code that pastes the file name to the first blank column and I'm not sure how to do that.
I'm not good with VBA and this is my first time posting on this forum, so please let me know if I didn't publish enough information. I was wondering if any of you could be able to help me with this? Here is the code that I currently have (Thank you!)
Sub CopyRange()
Application.ScreenUpdating = False
Dim wkbDest As Workbook
Dim wkbSource As Workbook
Set wkbDest = ThisWorkbook
Dim LastRow As Long
Const strPath As String = "C:\" 'file location
ChDir strPath
strExtension = Dir("*.xlsx")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPath & strExtension)
With wkbSource
LastRow = .Sheets("LX02 - Kostner WIP").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Sheets("Sheet1Source").Range("A2:L" & LastRow).Copy wkbDest.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
.Close savechanges:=False
End With
strExtension = Dir
Loop
Application.ScreenUpdating = True
End Sub
I'm trying to import data from several workbooks into a single one, but I'm trying to print the file name on the first blank column for every row of data. I want to do this step on the original files before importing the data to the new workbook.
The code that I have is able to paste all the information into one workbook, but I still need to create the part of the code that pastes the file name to the first blank column and I'm not sure how to do that.
I'm not good with VBA and this is my first time posting on this forum, so please let me know if I didn't publish enough information. I was wondering if any of you could be able to help me with this? Here is the code that I currently have (Thank you!)
Sub CopyRange()
Application.ScreenUpdating = False
Dim wkbDest As Workbook
Dim wkbSource As Workbook
Set wkbDest = ThisWorkbook
Dim LastRow As Long
Const strPath As String = "C:\" 'file location
ChDir strPath
strExtension = Dir("*.xlsx")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPath & strExtension)
With wkbSource
LastRow = .Sheets("LX02 - Kostner WIP").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Sheets("Sheet1Source").Range("A2:L" & LastRow).Copy wkbDest.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
.Close savechanges:=False
End With
strExtension = Dir
Loop
Application.ScreenUpdating = True
End Sub