Public Sub ImportExcelSheets()
Dim myPath(), j, iCnt as integer
myPath = Array("\\serverName\Year1.xlsx", "\\serverName\Year2.xlsx", "\\serverName\Year3.xlsx")
For j = LBound(myPath) To UBound(myPath)
if dir(mypath(j))<>"" then
Set objXL = CreateObject("Excel.Application")
objXL.Workbooks.Open myPath(j), , True
With objXL
For i = 1 To .Worksheets.Count
If InStr(.Worksheets(i).Name, "widget") Then
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
"tableName", myPath(j), True, .Worksheets(i).Name & "!A:M"
End If
Next
End With
objXL.Quit
Set objXL = Nothing
else
iCnt=iCnt + 1
end if
Next
if icnt=3 then
MsgBox "Worksheets do not exist. Exiting sub now!", vbInformation, "Error"
exit sub
end if
End Sub
Toggle HighlightingOpen in New WindowSelect All
|