I am trying to import a excel spreadsheet that has many sheets in it. I want to put each sheet into a diff table.
I am running this from vba code.
It imports the first sheet ok into a new table
but the second sheet it does not do it.
And the 2 table is filled with the same data from the first sheet.
here is my code
thanks for the help.
I am running this from vba code.
It imports the first sheet ok into a new table
but the second sheet it does not do it.
And the 2 table is filled with the same data from the first sheet.
here is my code
Code:
Sub read_excel_data()
Dim xlapp As Excel.Application
Dim xlwb As Excel.workbook
Dim sheet As Excel.Worksheet
Dim j As Integer
Dim sheetcnt As Integer
On Error GoTo err_startExcel
Set xlapp = GetObject(, "Excel.Application")
xlapp.Visible = False
Set xlwb = xlapp.Workbooks.Open("c:\ESE-FORM1043.NORTHEAST.xls")
sheetcnt = xlapp.ActiveWorkbook.Sheets.Count
'delete data from temp tables
DoCmd.OpenQuery "qry_maj_delete"
DoCmd.OpenQuery "qry_option_delete"
For z = 1 To sheetcnt
Set sheet = xlapp.ActiveWorkbook.Sheets(z)
If (sheet.Name = "MAJOR") Then
DoCmd.TransferSpreadsheet transfertype:=acImport, _
tablename:="maj", _
Filename:="c:\ESE-FORM1043.NORTHEAST.xls", Hasfieldnames:=False, _
Range:="b9:s210", SpreadsheetType:=acSpreadsheetTypeExcel9
End If
If (sheet.Name = "OPTION") Then
DoCmd.TransferSpreadsheet transfertype:=acImport, _
tablename:="OPT", _
Filename:="c:\ESE-FORM1043.NORTHEAST.xls", Hasfieldnames:=False, _
Range:="b9:s53", SpreadsheetType:=acSpreadsheetTypeExcel9
End If
Next z
'sheet.Application.ActiveWorkbook.Save
sheet.Application.ActiveWorkbook.Close
xlapp.Quit
Set xlapp = Nothing
Set xlwb = Nothing
Set sheet = Nothing
End
Exit Sub
err_startExcel:
If Err.Number = 429 Then 'No current instance of Excel start up Excel
Set xlapp = GetObject("", "Excel.Application")
Resume Next
Else
MsgBox Err.Description, vbExclamation, "Error: " & Err.Number, Err.HelpFile, Err.HelpContext
Exit Sub
End If
End Sub
thanks for the help.