Building on @chrisneilsen 's solution, here'a more compact code:
Option Explicit
Public Sub myImport()
Dim sPathName As String, sFileName As String
Dim targetSht As Worksheet
Set targetSht = ActiveWorkbook.Worksheets("Master UTP")
sPathName = ThisWorkbook.Path & "\"
sFileName = Dir(sPathName & "*.xls", vbNormal)
Do While Len(sFileName) > 0
If sFileName <> targetSht.Parent.Name Then
On Error Resume Next
With Workbooks.Open(sPathName & sFileName)
.Sheets("UTP").Copy After:=targetSht
.Close False
End With
On Error GoTo 0
End If
sFileName = Dir
Loop
End Sub
which should be even slightly more compacted if it can be safely assumed that ActiveWorkbook
is a "macro" one, i.e. with a "xlsm" type in its name, so that it can never match any "xls" name:
Option Explicit
Public Sub myImport()
Dim sPathName As String, sFileName As String
Dim targetSht As Worksheet
Set targetSht = ActiveWorkbook.Worksheets("Master UTP")
sPathName = ThisWorkbook.Path & "\"
sFileName = Dir(sPathName & "*.xls", vbNormal)
Do While Len(sFileName) > 0
On Error Resume Next
With Workbooks.Open(sPathName & sFileName)
.Sheets("UTP").Copy After:=targetSht
.Close False
End With
On Error GoTo 0
sFileName = Dir
Loop
End Sub
Finally, you could appreciate eliminate the flickering at any xls file opening, so you maight enclose the loop inside Application.ScreenUpdating = False/True
statements:
Option Explicit
Public Sub myImport()
Dim sPathName As String, sFileName As String
Dim targetSht As Worksheet
Set targetSht = ActiveWorkbook.Worksheets("Master UTP")
sPathName = ThisWorkbook.Path & "\"
sFileName = Dir(sPathName & "*.xls", vbNormal)
Application.ScreenUpdating = False
Do While Len(sFileName) > 0
On Error Resume Next
With Workbooks.Open(sPathName & sFileName)
.Sheets("UTP").Copy After:=targetSht
.Close False
End With
On Error GoTo 0
sFileName = Dir
Loop
Application.ScreenUpdating = True
End Sub