Merging Workbooks into Master & move the subworkbooks into other folder

dellzy

Board Regular
Joined
Apr 24, 2013
Messages
146
Hi Excel Experts,

Am trying to code down macros that can do the following:-

1. Copy sheet from multi workbooks (in same folder) into 1 main (Compiler) excel workbook (separated by tabs)
2. Each time after the sheet is copied into Compiler workbook, the source file should be moved to another folder titled as "Done" in the same path.
3. The multisheets' tabs in Compiler workbook should then be merged into 1 Master worksheet (Sheet1 in same Compiler workbook). Before merging, must check first if Master worksheet has been created or not. If yes, the data range to be added must continue from the last row of data in Master. Else, a new worksheet named "Master" must automatically be created and all data range will be added.
4. After merged, all worksheets except Master and Sheet2 must be deleted.


I've managed to do for step 1 and 4 while partly on step 3 and none for 2. Step 2 should be done while in loop of Step 1.

My code below are for Step 1 and partly Step 3. I need help to modify my Step 1 coding to be able to do Step 1 & 2. Then to modify my Step 3 coding which mainly only able to merge all content of multiworksheets but cannot copy the first header as the main header in Master file. Apart from that, currently my coding cannot be able to check the existence of Master worksheet before proceed to merge and it will not auto create new worksheet as Master if found not exist.

Appreciate so much of your help.

STEP 1 (need to code to allow doing STEP 2)
Code:
Sub MergeWbooks()
Dim myPath As String
Dim FileName As String
On Error GoTo Errorcatch


myPath = ThisWorkbook.Path & "\"
FileName = Dir(myPath & "*.csv")
  Do While FileName <> ""
  Workbooks.Open FileName:=myPath & FileName, ReadOnly:=True
  
     For Each Sheet In ActiveWorkbook.Sheets

     LRow = ActiveSheet.Range("B1", ActiveSheet.Range("B1").End(xlDown)).Rows.Count

    Sheet.Copy After:=ThisWorkbook.Sheets(1)
    

     Workbooks(FileName).Close savechanges:=False
  Next Sheet

     FileName = Dir()
  Loop

Errorcatch:
MsgBox Err.Description
Call InsertSheetName
End Sub

Partly STEP 3 (need code to check existence of Master worksheet. If exist, continue from last row data. If not, create a new one).

Code:
Sub MergeWsheets2Master()
    Dim wrk As Workbook
    Dim sht As Worksheet
    Dim rng As Range
    Dim colCount As Integer
     
    Set wrk = ActiveWorkbook
     
 
    Application.ScreenUpdating = False
     Worksheets("Master").Select
    Set sht = wrk.Worksheets(3)
    colCount = sht.Cells(1, 255).End(xlToLeft).Column
    ActiveSheet.Cells(1, 1).Resize(1, colCount).Value = sht.Cells(1, 1).Resize(1, colCount).Value

    For Each sht In wrk.Worksheets
        If sht.Index = wrk.Worksheets.Count + 1 Then
            Exit For
        End If
        Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(1048576, 1).End(xlUp).Resize(2, colCount))

        ActiveSheet.Cells(1048576, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
    Next sht
    ActiveSheet.Columns.AutoFit
     
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Dear All,

I managed to find a solution for this by modifying the code to work based on the worksheet index. Now, all done successfully.

This considered solved.

Thank you.

DZ
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,352
Members
449,080
Latest member
Armadillos

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