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)
Partly STEP 3 (need code to check existence of Master worksheet. If exist, continue from last row data. If not, create a new one).
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