Printing file name to last blank column before importing to new workbook

seacubs17

New Member
Joined
Jan 22, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
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
 
try
VBA Code:
wkbSource.Sheets("Sheet1Source").Range(Cells(2, 12),Cells(LastRow,12)).Value = strPath & strExtension
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How about
VBA Code:
    strExtension = Dir("*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
            .Sheets("Sheet1").Range("A2:L" & LastRow).Copy wkbDest.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        End With
        With wkbDest.Sheets("Sheet1")
            .Range("L" & Rows.Count).End(xlUp).Offset(1).Resize(LastRow - 1).Value = wkbSource.FullName
        End With
        wkbSource.Close savechanges:=False

        strExtension = Dir
    
    Loop
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,330
Members
449,155
Latest member
ravioli44

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