VBA Help for Combining Multiple Workbooks into One Worksheet.

Jambi46n2

Active Member
Joined
May 24, 2016
Messages
260
Office Version
  1. 365
Platform
  1. Windows
Code:
    Dim wkbDest As Workbook    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Dim LastRow As Long
    Dim strPath As String
    strPath = Range("G30").Value
    ChDir strPath
    Dim strExtension As String
    strExtension = ("*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("SQL Pull").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Sheets("SQL Pull").Range("A2:AV" & LastRow).Copy wkbDest.Sheets("Combined SQL Pull").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            .Close savechanges:=False
        End With
[COLOR=#ff0000]        strExtension = Dir '<----- Run Time Error 5 here. "Invalid Procedure Call or Argument" [/COLOR]
    Loop

The Code Above was intended to grab every file with the extension .xlsx from the folder path pasted in cell G30. Then find the worksheet named "SQL Pull", copy all the data minus the header, and paste into the workbook that is currently opened into the sheet "Combined SQL Pull". The code works for a little bit, then errors on the line in red above.

Can someone please assist on what needs to be done to make this work correctly?

Thank you in advance.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
it should be
Code:
Dir[COLOR=#ff0000]()[/COLOR]
 
Upvote 0
Thank you for the reply!

I changed it to the following, but still get the same error on the same line:

Code:
    Dim wkbDest As Workbook    
    Dim wkbSource As Workbook
    Dim LastRow As Long
    Dim strPath As String
    Dim strExtension As String
    
    Set wkbDest = ThisWorkbook
    strPath = Range("G30").Value
    ChDir strPath
    strExtension = ("*.xlsx*")
    
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("SQL Pull").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Sheets("SQL Pull").Range("A2:AV" & LastRow).Copy wkbDest.Sheets("Combined SQL Pull").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            .Close savechanges:=False
        End With
[COLOR=#ff0000]        strExtension = Dir()[/COLOR]
    Loop
 
Last edited:
Upvote 0
You also need to change
Code:
strExtension = [COLOR=#ff0000]Dir(strPath & [/COLOR]"*.xlsx")
Do While strExtension <> ""
 
Upvote 0
You also need to change
Code:
strExtension = [COLOR=#ff0000]Dir(strPath & [/COLOR]"*.xlsx")
Do While strExtension <> ""


This worked!!

You sir are a life saver. I can't thank you enough :-)
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Looks like I've come across another error that is unrelated to my original request.

"Can't paste because the copy area and paste area aren't the same size"

Don't suppose a good workaround for this?

I suspect this line needs to be modified:

Code:
.Sheets("SQL Pull").Range("A2:AV" & LastRow).Copy wkbDest.Sheets("Combined SQL Pull").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
 
Upvote 0
Do you have any merged cells in either sheet?
Also do you use Text to Columns?
 
Upvote 0
Add the line in blue
Code:
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = .Sheets("SQL Pull").Cells.find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            [COLOR=#0000ff]MsgBox LastRow & vbLf & wkbDest.Sheets("Combined SQL Pull").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row[/COLOR]
            .Sheets("SQL Pull").Range("A2:AV" & LastRow).Copy wkbDest.Sheets("Combined SQL Pull").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            .Close savechanges:=False
        End With
        strExtension = Dir()
    Loop
What does the message box say just before the code fails?
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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