Option Compare Database
Option Explicit
Dim xlfilename As String
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim iRow As Integer
Dim iCol As Integer
Dim iLastRow
Dim cValue As String
Dim cGroupValue As String
Dim cTaskCode_Row As Integer
Dim cTaskCode As String
Private Const xlCellTypeLastCell As Long = 11
Dim ActiveSheet As Property
Function LoadExcelSpreadsheet(xlfilename)
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(xlfilename)
Set oSheet = oBook.Worksheets(1)
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("ImportTable")
iRow = oSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
iCol = oSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
With ActiveSheet.UsedRange '>>>> ".UsedRange" error:"Method or data member not found"
iCol = .Cells(1, 1).Column + .Columns.Count - 1
iLastRow = .Cells(1, 1).Row + .Rows.Count - 1
End With
'iRow is the last row in the worksheet that contains data
For nRow = 1 To iRow
cGroupValue = oSheet.Range("D" & nRow).Value ''' Column where "Task Code" resides
If InStr(1, cGroupValue, "Task Code :") = 1 Then
'''Extract Task Code as a string for values following colon ": "
cTaskCode = Right(cGroupValue, Len(cGroupValue) - InStr(cGroupValue, ": "))
End If
cValue = oSheet.Range("G" & nRow).Value ''' "Initialized" data field is never empty
''' "Initialized" is the field heading in the MXi file
If cValue <> "" Or cValue <> ("Initialized") Then
rs.AddNew
rs(TaskCode) = cTaskCode
rs(Aircraft) = oSheet.Range("A" & nRow).Value
rs(Rego) = oSheet.Range("B" & nRow).Value
rs(EngineAPU_PN) = oSheet.Range("C" & nRow).Value
rs(EngineAPU_SN) = oSheet.Range("D" & nRow).Value
rs(Component_PN) = oSheet.Range("E" & nRow).Value
rs(Component_SN) = oSheet.Range("F" & nRow).Value
rs(Initialised) = oSheet.Range("G" & nRow).Value
rs.Update
End If
Next
rs.Close
oExcel.Quit
Set oExcel = Nothing
Set oBook = Nothing
Set oSheet = Nothing
Set rs = Nothing
End Function