VBA code to combine multiple workbooks and record name

SunshineVBA

New Member
Joined
May 21, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hello,

I’m looking for a piece of code that will go through all the files in a folder, copy data from tab “CoverPage” for range at A14:B and down (there could be 1 line or could be up to 15, not consistent). Then paste it into the main workbook and worksheet “Allaccounts”.

So I already found code that works perfect for this part. But I am wanting to also record the name of the source workbook beside any lines of data that it copied. Wondering if anyone can help me with this piece of code?

Thank you
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I already found code that works perfect for this part.
I know you are referring to this: VBA to copy data from multiple workbooks into master sheet
But please use the actual code you are using (and use the VBA tag in the toolbar) and then we can modify if acordingly.

Do you want the workbook name to the left or right of what is being pasted in ?
Do you want the xlsx or whatever the extension is as part of the workbook name or just the name ?
 
Upvote 0
I know you are referring to this: VBA to copy data from multiple workbooks into master sheet
But please use the actual code you are using (and use the VBA tag in the toolbar) and then we can modify if acordingly.

Do you want the workbook name to the left or right of what is being pasted in ?
Do you want the xlsx or whatever the extension is as part of the workbook name or just the name ?

Below is the code I’m using that I got from the other thread. This is working perfect for putting the range I need into columns B and C in the main workbook. I’d ideally like to put the name of the file in column A beside all data that came from that file.

As for the tag, I’m unsure how to do that. Will try and figure it out.

Code:
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:\Users\xbv\Desktop\group1\"
ChDir strPath
strExtension = Dir("*.xls*")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPath & strExtension)
With wkbSource
LastRow = .Sheets("CoverPage”).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Sheets("CoverPage”).Range(“A14:B” & LastRow).Copy wkbDest.Sheets("AllAcxounts").Cells(Rows.Count, "B”).End(xlUp).Offset(1, 0)
.Close savechanges:=False
End With
strExtension = Dir
Loop
Application.ScreenUpdating = True
End Sub
 
Upvote 0
As for the tag, I’m unsure how to do that.
Just click on the VBA button in the toolbar and paste the code between code tages that appear.

See if the below works for you.

VBA Code:
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Dim LastRow As Long, destNextRow As Long, destLastRow As Long
    Dim strExtension As String
    Set wkbDest = ThisWorkbook
   

    Const strPath As String = "C:\Users\xbv\Desktop\group1\"
    ChDir strPath
    strExtension = Dir("*.xls*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("CoverPage").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            destNextRow = wkbDest.Sheets("AllAcxounts").Cells(Rows.Count, "B").End(xlUp).Row + 1
            .Sheets("CoverPage").Range("A14:B" & LastRow).Copy wkbDest.Sheets("AllAcxounts").Cells(destNextRow, "B")
            ' Add Source Workbook Name
            destLastRow = wkbDest.Sheets("AllAcxounts").Cells(Rows.Count, "B").End(xlUp).Row
            wkbDest.Sheets("AllAcxounts").Range(Cells(destNextRow, "A"), Cells(destLastRow, "A")).Value = wkbSource.Name
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Just click on the VBA button in the toolbar and paste the code between code tages that appear.

See if the below works for you.

VBA Code:
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Dim LastRow As Long, destNextRow As Long, destLastRow As Long
    Dim strExtension As String
    Set wkbDest = ThisWorkbook
  

    Const strPath As String = "C:\Users\xbv\Desktop\group1\"
    ChDir strPath
    strExtension = Dir("*.xls*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("CoverPage").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            destNextRow = wkbDest.Sheets("AllAcxounts").Cells(Rows.Count, "B").End(xlUp).Row + 1
            .Sheets("CoverPage").Range("A14:B" & LastRow).Copy wkbDest.Sheets("AllAcxounts").Cells(destNextRow, "B")
            ' Add Source Workbook Name
            destLastRow = wkbDest.Sheets("AllAcxounts").Cells(Rows.Count, "B").End(xlUp).Row
            wkbDest.Sheets("AllAcxounts").Range(Cells(destNextRow, "A"), Cells(destLastRow, "A")).Value = wkbSource.Name
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
I am getting a runtime (1004) error on line

WkbDest.sheets(“AllAccounts”).Range(Cells(destNextRow, “A”, Cells(destLastTow, “A”)),Value = wkbSource.Name
 
Upvote 0
I am unable to replicate that please click on the "VBA" button in the toolbar and paste your full code at the insertion point being between "]" & "[".
 
Upvote 0
As for the tag, I’m unsure how to do that. Will try and figure it out.
Have a look here How to Post Your VBA Code

How about
VBA Code:
wkbDest.Sheets("AllAcxounts").Range(wkbDest.Sheets("AllAcxounts").Cells(destNextRow, "A"), wkbDest.Sheets("AllAcxounts").Cells(destLastRow, "A")).Value = wkbSource.Name
 
Upvote 0
VBA Code:
Sub CopyRange()

Application.ScreenUpdating = False

Dim wkbDest As Workbook

Dim wkbSource As Workbook

Dim LastRow As Long, destNextRow As Long, destLastRow As Long

Dim strExtension As String

Set wkbDest = ThisWorkbook



Const strPath As String = "C:\Users...\WP from sharepoint\Test\"

ChDir strPath

strExtension = Dir("*.xlsb*")

Do While strExtension <> ""

Set wkbSource = Workbooks.Open(strPath & strExtension)

With wkbSource

LastRow = .Sheets("CoverPage").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

destNextRow = wkbDest.Sheets("AllAccounts").Cells(Rows.Count, "B").End(xlUp).Row + 1

.Sheets("CoverPage").Range("A14:B" & LastRow).Copy wkbDest.Sheets("AllAccounts").Cells(destNextRow, "B")

' Add Source Workbook Name

destLastRow = wkbDest.Sheets("AllAccounts").Cells(Rows.Count, "B").End(xlUp).Row

wkbDest.Sheets("AllAccounts").Range(Cells(destNextRow, "A"), Cells(destLastRow, "A")).Value = wkbSource.Name

.Close savechanges:=False

End With

strExtension = Dir

Loop

Application.ScreenUpdating = True

End Sub
 
Last edited by a moderator:
Upvote 0
At some point you changed (corrected) your sheet name from AllAcxounts to AllAccounts and this is what has caused the error in the previous post using Fluff's more explicit code.
This is just a slight variation of his so replace this line:
VBA Code:
wkbDest.Sheets("AllAccounts").Range(Cells(destNextRow, "A"), Cells(destLastRow, "A")).Value = wkbSource.Name

With these 3 lines:
VBA Code:
            With wkbDest.Sheets("AllAccounts")
                .Range(.Cells(destNextRow, "A"), .Cells(destLastRow, "A")).Value = wkbSource.Name
            End With

Given you are in a different time zone and to keep it moving, if that doesn't work put this before that With wkbDest line and send us a picture of the last time it displays.
VBA Code:
            MsgBox Prompt:="destNextRow: " & destNextRow & vbTab & "destLastRow: " & destLastRow
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,150
Messages
6,123,312
Members
449,094
Latest member
Chestertim

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