combine multiple workbooks first sheet start at row 2 and appending to the bottom

jordanburch

Active Member
Joined
Jun 10, 2016
Messages
440
Office Version
  1. 2016
Hey guys

been trying different codes most of the day. Have not found anything that would work. I would like it to combine multiple spreadsheets starting with the name Phase1. I would then like it to create a column with the file title name to the left to remeber which file the record came from. I also would like it to start on row 2. I have had some success but nothing seems to pop up with everything I need. Can anyone help?

Jordan
 
Try the following.
In a book put the macro and create a sheet called "summary".
Change "books" to the folder where you have the books.

VBA Code:
Sub combine_multiple_workbooks()
  Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet
  Dim sFile As Variant, sPath As String, LastRow1 As Long, LastRow2 As Long
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set wb1 = ThisWorkbook
  Set sh1 = wb1.Sheets("Summary")
  sh1.Cells.ClearContents
  sh1.Range("A1").Value = "File"
  sPath = "C:\books\"
  sFile = Dir(sPath & "Phase1*.xls*")
  
  Do While sFile <> ""
    Set wb2 = Workbooks.Open(sPath & sFile)
    Set sh2 = wb2.Sheets(1)
    LastRow2 = sh2.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
    LastRow1 = sh1.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
    sh2.Range("A2:AE" & LastRow2).Copy
    sh1.Range("B" & LastRow1).PasteSpecial xlPasteValues
    sh1.Range("A" & LastRow1).Resize(LastRow2 - 1).Value = sFile
    wb2.Close False
    sFile = Dir()
  Loop
End Sub
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
OK I will try this out. There is always data in column B fyi. sorry I had to restart my laptop and it took forever.
 
Upvote 0
ok so it did work. Thanks again for that. It looks like its erroring out because the file is slightly different. The only difference I noticed was that instead of starting on row 2 it started on row 3. So I deleted row 1 for them to look the same and it still gives the same error. There are some hidden cells and columns in the file it is erroring out in, could that be the problem?

The error message is

object variable or with block variable not set
 
Upvote 0
Try this
VBA Code:
Sub combine_multiple_workbooks()
  Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet
  Dim sFile As Variant, sPath As String, LastRow1 As Long, LastRow2 As Long
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set wb1 = ThisWorkbook
  Set sh1 = wb1.Sheets("Summary")
  sh1.Cells.ClearContents
  sh1.Range("A1").Value = "File"
  sPath = "C:\books\"
  sFile = Dir(sPath & "Phase1*.xls*")
  On Error GoTo nextfile
  Do While sFile <> ""
    Set wb2 = Workbooks.Open(sPath & sFile)
    Set sh2 = wb2.Sheets(1)
    LastRow2 = sh2.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
    LastRow1 = sh1.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
    sh2.Range("A2:AE" & LastRow2).Copy
    sh1.Range("B" & LastRow1).PasteSpecial xlPasteValues
    sh1.Range("A" & LastRow1).Resize(LastRow2 - 1).Value = sFile
nextfile:
    wb2.Close False
    sFile = Dir()
  Loop
End Sub
 
Upvote 0
Probably coming from this line
VBA Code:
    LastRow1 = sh1.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
As sh1 is blank the first time through the Do loop.
 
Upvote 0
it seemed like it skipped a file. The last time it errored out on this particular file and this time it looks like it skipped it all together. I dont know if that helps any. Im trying to do my best but im not near as advanced as you fellas. Hopefully this information helps.
 
Upvote 0

Forum statistics

Threads
1,214,988
Messages
6,122,620
Members
449,092
Latest member
amyap

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