Adding the workbook name to column F VBA

fawnlemur

New Member
Joined
Dec 9, 2018
Messages
29
Hi

I'm using the below code to copy data from multiple spreadsheets into a master spreadsheet. It works fine but I want to add a column (F) to state which workbook it was copied from. can some one please help me with that.

so if data was copied from workbook1 column F would say workbook1.

Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Dim LastRow As Long
    Dim i As Long
    i = 999999
    Range("A2:G" & i).Clear 'ClearContents
    Const strPath As String = "Link Here"
    ChDir strPath
    strExtension = Dir("*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("CASE LOG").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Sheets("CASE LOG").Range("A3:E" & LastRow).Copy wkbDest.Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
     
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Dim LastRow As Long, LastRow2 As Long
    Dim i As Long
    i = 999999
    Range("A2:G" & i).Clear 'ClearContents
    Const strPath As String = "Link Here"
    ChDir strPath
    strExtension = Dir("*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("CASE LOG").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            LastRow2 = wkbDest.Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
            .Sheets("CASE LOG").Range("A3:E" & LastRow).Copy wkbDest.Sheets("Master").Cells(LastRow2, "A")
            wkbDest.Sheets("Master").Cells(LastRow2, "F").Resize(LastRow - 2).Value = .Name
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
Application.ScreenUpdating = True
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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