Hello everyone.
I 've multiple workbooks with similar structure. Apart from number of sheets with random names all of them have three sheets with names "Mastersheet", "Control" and "Response". All files in one directory. I found VBA Macro to merge multiple workbooks but I could not incorporate "If" condition to avoid merging sheets with names "Mastersheet", "Control" and "Response". It gives error message. I'd be grateful for help with this macro.
I 've multiple workbooks with similar structure. Apart from number of sheets with random names all of them have three sheets with names "Mastersheet", "Control" and "Response". All files in one directory. I found VBA Macro to merge multiple workbooks but I could not incorporate "If" condition to avoid merging sheets with names "Mastersheet", "Control" and "Response". It gives error message. I'd be grateful for help with this macro.
VBA Code:
Sub MergeExcelFiles()
Dim fnameList, fnameCurFile As Variant
Dim countFiles, countSheets As Integer
Dim wksCurSheet As Worksheet
Dim wbkCurBook, wbkSrcBook As Workbook
fnameList = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", Title:="Choose Excel files to merge", MultiSelect:=True)
If (vbBoolean <> VarType(fnameList)) Then
If (UBound(fnameList) > 0) Then
countFiles = 0
countSheets = 0
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set wbkCurBook = ActiveWorkbook
For Each fnameCurFile In fnameList
countFiles = countFiles + 1
Set wbkSrcBook = Workbooks.Open(Filename:=fnameCurFile)
If wksCurSheet.Name <> "MasterSheet" And wksCurSheet.Name <> "Control" And wksCurSheet.Name <> "Response" Then
For Each wksCurSheet In wbkSrcBook.Sheets
countSheets = countSheets + 1
wksCurSheet.Copy after:=wbkCurBook.Sheets(wbkCurBook.Sheets.Count)
Next
End If
wbkSrcBook.Close SaveChanges:=False
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Processed " & countFiles & " files" & vbCrLf & "Merged " & countSheets & " worksheets", Title:="Merge Excel files"
End If
Else
MsgBox "No files selected", Title:="Merge Excel files"
End If
End Sub