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
 

dellzy

Board Regular
Joined
Apr 24, 2013
Messages
146
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
 

Forum statistics

Threads
1,082,152
Messages
5,363,453
Members
400,737
Latest member
vipamuk

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top