PippaThePointer
New Member
- Joined
- Sep 21, 2023
- Messages
- 31
- Office Version
- 2016
- Platform
- Windows
Hi,
I need to consolidate a number of worksheets from multiple workbooks into my template. I have found and tested lots of VBA code that almost does what i want. So far I am doing it it 2 steps.
The code that seems to give me the best results for step one is below but It works great if the source file only has one worksheet, but it fails if it has more than one worksheet. It cant seem to resolve the range when more than one sheet. It will work with multiple worksheets if i disble the function to add the worksheet name into new column. Other options i have tried is to copy the sheets but rename each worksheet to include the file name. Then my next macro could use that when consolidating into one sheet. However i have not succeded in this either.
If anyone could help me edit this code so it loops correctly it would be wonderful.
Sub MergeExcelFilesWithFileName()
Dim fnameList, fnameCurFile As Variant
Dim countFiles, countSheets As Integer
Dim wksCurSheet As Worksheet
Dim wbkCurBook, wbkSrcBook As Workbook
Dim c As Long, r As Long
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)
For Each wksCurSheet In wbkSrcBook.Sheets
countSheets = countSheets + 1
c = ActiveSheet.UsedRange.Columns.Count
r = ActiveSheet.UsedRange.Rows.Count
'change Split value to get part of file name - 1 is second part
'This fails if more than one sheet in the source file.
wksCurSheet.Range(Cells(1, c + 1), Cells(r, c + 1)).Value = Split(wbkSrcBook.Name, " ")(1)
'add header - This works
wksCurSheet.Cells(1, c + 1).Value = "Store"
'Copy worksheet
wksCurSheet.Copy After:=wbkCurBook.Sheets(wbkCurBook.Sheets.Count)
Next
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
I need to consolidate a number of worksheets from multiple workbooks into my template. I have found and tested lots of VBA code that almost does what i want. So far I am doing it it 2 steps.
- copy all the selected files tabs into my template so it has lots of copied worksheets but add a row with file name or rename copied tab
- consolildate all the worksheets into a new worksheet and remove all rows with blank 'qty'
The code that seems to give me the best results for step one is below but It works great if the source file only has one worksheet, but it fails if it has more than one worksheet. It cant seem to resolve the range when more than one sheet. It will work with multiple worksheets if i disble the function to add the worksheet name into new column. Other options i have tried is to copy the sheets but rename each worksheet to include the file name. Then my next macro could use that when consolidating into one sheet. However i have not succeded in this either.
If anyone could help me edit this code so it loops correctly it would be wonderful.
Sub MergeExcelFilesWithFileName()
Dim fnameList, fnameCurFile As Variant
Dim countFiles, countSheets As Integer
Dim wksCurSheet As Worksheet
Dim wbkCurBook, wbkSrcBook As Workbook
Dim c As Long, r As Long
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)
For Each wksCurSheet In wbkSrcBook.Sheets
countSheets = countSheets + 1
c = ActiveSheet.UsedRange.Columns.Count
r = ActiveSheet.UsedRange.Rows.Count
'change Split value to get part of file name - 1 is second part
'This fails if more than one sheet in the source file.
wksCurSheet.Range(Cells(1, c + 1), Cells(r, c + 1)).Value = Split(wbkSrcBook.Name, " ")(1)
'add header - This works
wksCurSheet.Cells(1, c + 1).Value = "Store"
'Copy worksheet
wksCurSheet.Copy After:=wbkCurBook.Sheets(wbkCurBook.Sheets.Count)
Next
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