Consolidation
Consolidation
Dim i As Integer
With FileDialog
.AllowMultiSelect = True
.Filters.Clear
End If
End With
For i = 1 To FileDialog.SelectedItems.Count
FilePath = FileDialog.SelectedItems(i)
On Error Resume Next ' Skip error for this block and handle it later
GoTo NextFile ' If file cannot be opened, skip to the next one
End If
On Error GoTo ErrorHandler ' Reset error handling for other operations
wsName = wsSource.Name ' Get the name of the worksheet in the source workbook
' Check if a sheet with the same name exists in the master workbook
wsMaster.Name = wsName
End If
colCount = rngSource.Columns.Count
On Error Resume Next ' Handle any error that may occur during copying
rngSource.Copy
MsgBox "Error copying data from sheet " & wsSource.Name & " in file " & FileName,
vbCritical
Err.Clear
End If
' Add the file name and sheet name in the next two columns
End If
NextSheet:
Next wsSource
NextFile:
Next i
' Clean up
Application.CutCopyMode = False
ErrorHandler:
If Not wbSource Is Nothing Then wbSource.Close False ' Close workbook if it's open
Application.CutCopyMode = False
End Sub