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
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
How about:

Change "Summary" and "C:\books\" for your data.

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
  Dim LastRow1 As Long, LastRow2 As Long, LastCol2 As Long
  Application.ScreenUpdating = False
  
  Set wb1 = ThisWorkbook
  Set sh1 = wb1.Sheets("Summary")
  sh1.Cells.ClearContents
  sh1.Range("A1").Value = "File"
  sPath = "C:\books\"
  sFile = Dir(sPath & "*.xls*")
  
  Do While sFile <> ""
    Set wb2 = Workbooks.Open(sPath & sFile)
    For Each sh2 In wb2.Sheets
      If UCase(Left(sh2.Name, 6)) = UCase("Phase1") Then
        LastRow2 = sh2.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
        LastCol2 = sh2.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Column
        LastRow1 = sh1.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
        sh2.Range("A2", sh2.Cells(LastRow2, LastCol2)).Copy
        sh1.Range("B" & LastRow1).PasteSpecial xlPasteValues
        sh1.Range("A" & LastRow1).Resize(LastRow2 - 1).Value = sFile
      End If
    Next
    wb2.Close False
    sFile = Dir()
  Loop
End Sub
 
Upvote 0
Thanks Dante, I really appreciate that. For some reason it is erroring out on the Summary part I even changed my tabs to that name on the blank workbook that I want to import everything to. I also tried to change it to the name of the source workbooks and it still errored out there. The source workbooks have several different tab names, so I would like to import the first tab of each workbook only. It does not matter the name of the tab on the file which im importing everything to. Could you please help me make those updates? THANKS again!
 
Upvote 0
. I would like it to combine multiple spreadsheets starting with the name Phase1.
What does the error say?
It would help if you specify which columns you want to copy.
In which column do you always have data?
Your original request says "phase" sheet.
Please your comments.
 
Upvote 0
oh ok thats the issue then my bad. I want the spreadsheets imported only with the name Phase1. Then inside that spreadsheet I only want the first sheet copied over onto the master file. I want columns a:ae copied over starting at row 2 and pasting with the starting row 2, but the next file would simply find the last row and then append to that and would copy from the source file starting at row 2 and append it to whereever the file left off from the file before. Does that make sense?
 
Upvote 0
In columns A:AE is where the data is.

im sorry man I keep saying spreadsheets I meant to say workbooks.

IE

Phase1_cl.xls
Phase1_co.xls
Phase1_IN.xls

The only constant is the Phase1 that is the name of the workbook not spreadsheet. I would like only the first tab inside the workbook. The name of that tab changes across each workbook.

hopefully that makes sense.

Thanks again

Jordan
 
Upvote 0
In which column do you always have data?
Please answer
 
Upvote 0
In columns A:AE is where the data is.

Sorry, I didn't explain myself. I need to know if any of these columns always have data, for example column A always has data or some cells are empty, so column B always has data or some cells are empty, then C always has data. Does any column always have data?
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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