Question : VBA to import specific Excel WKSTs into Access database.

I’m using Office 2007, and I have an excel file with 4 worksheet tabs—1 entitled “Main”, and the other 3 entitled “Widget1”, “Widget2”, and “Widget3”.  

I would like to import data into Access from all worksheet tabs whose name starts with “Widget” from column A to D in one table call ‘tblMaster’.

The restrictions should would be to exclude importing row 1 from any of the sheets, and exclude importing any rows without any value listed under column A (blank or null), and exclude rows that have a value of ‘YZ’ under column D.

Is there any way I can do this with VBA?  My example is easy enough to accomplish through linking the sheets to the database and writing a union query to achieve the expected results.  My real situation has dozens of sheet tabs, which is why I prefer to tackle this with VBA.  Any insight is much appreciated.  Thanks!    

Answer : VBA to import specific Excel WKSTs into Access database.

try this codes,to import all sheets with "widget" in the sheet name

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
Sub ImportAllSheets()
Dim objXL As Object
Dim sTable, xlPath As String, i As Integer

xlPath = "C:\myExcel.xls"

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, _
             "TestTable", xlPath, True, .Worksheets(i).Name & "!A:D"
            end if
        Next
    End With
    objXL.Quit
    Set objXL = Nothing

End Sub
Random Solutions  
 
programming4us programming4us