Question : VBA code to loop through a process for importing EXISTING excel files.

If you examine my block of code below, you’ll notice I’m importing spreadsheets whose name starts with “widget” from 3 different excel files (Year1.xlsx, Year2.xlsx, Year3.xlsx).  

I’m looking for a more efficient way to write this code to eliminate some of the redundancy.  You’ll notice for subs “ImportSheetsYears1”, “ImportSheetsYears2”, and “ImportSheetsYears3” that the code is identical except for the ‘xlPath’.  So I probably need some sort of loop, but I’m not sure how to code it.

Also in the loop, if the excel file does not exist; it should skip it and go to the next excel file.  If none of the specified excel files exist, a msgbox should fire and the sub should be exited.  I’m assuming an error handler would be the best way to go about this?  

Anyhow, thanks in advance for any possible solutions.  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
Option Compare Database
Option Explicit

Dim objXL As Object
Dim sTable, xlPath As String, i As Integer


Public sub ImportExcelSheets()
Call ImportSheetsYears1
Call ImportSheetsYears2
Call ImportSheetsYears3
End Sub




Private Sub ImportSheetsYears1()

xlPath = "\\serverName\Year1.xlsx"

Set objXL = CreateObject("Excel.Application")
    objXL.Workbooks.Open xlPath, , True
    With objXL
        For i = 1 To .Worksheets.Count
            If InStr(.Worksheets(i).Name, "widget") Then
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
             "tableName", xlPath, True, .Worksheets(i).Name & "!A:M"
            End If
        Next
    End With
    objXL.Quit
    Set objXL = Nothing

End Sub

Private Sub ImportSheetsYears2()

xlPath = "\\serverName\Year2.xlsx"

Set objXL = CreateObject("Excel.Application")
    objXL.Workbooks.Open xlPath, , True
    With objXL
        For i = 1 To .Worksheets.Count
            If InStr(.Worksheets(i).Name, "widget") Then
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
             "tableName", xlPath, True, .Worksheets(i).Name & "!A:M"
            End If
        Next
    End With
    objXL.Quit
    Set objXL = Nothing

End Sub


Private Sub ImportSheetsYears3()

xlPath = "\\serverName\Year3.xlsx"

Set objXL = CreateObject("Excel.Application")
    objXL.Workbooks.Open xlPath, , True
    With objXL
        For i = 1 To .Worksheets.Count
            If InStr(.Worksheets(i).Name, "widget") Then
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
             "tableName", xlPath, True, .Worksheets(i).Name & "!A:M"
            End If
        Next
    End With
    objXL.Quit
    Set objXL = Nothing

End Sub

Answer : VBA code to loop through a process for importing EXISTING excel files.

I'm sure there is... but as I stated, it's not reasonable.  How could you do it?  As I told someone else asking a question of a similar nature, hire a programmer/hacker who can analyze how Windows does the upgrade to 32 bit, what calls it makes, and so-on and so-forth, and eventually, I'm sure they'll figure out a way.  It may costs 5 or 6 figures... but if you've got the money, nothing is impossible.

Put simply, as others and myself have stated in one way or another - and I'll restate for a second time in this comment - there is NO REASONABLE way of doing this.  That is your answer.  No reasonable technician (as I would define them) would spend any significant time trying to do this so it's HIGHLY unlikely you'll find ANYONE, especially anyone here, who has found a way of doing it - reasonable or otherwise.  Experts-Exchange is a great place to find answers to questions and problems that impact a business or your ability to use a system.  When you want to do something that no one thinks is a good idea or even possible, you'll almost certainly not find an answer you're satisfied with even when the answers provided are, lacking further evidence to the contrary, 100% accurate.
Random Solutions  
 
programming4us programming4us