I've been trying to debug a piece of VBA code which opens an Excel file from Access and selects certain data to record into an table.
I have been receiving an intermittent "Object Variable or With Block Variable not set" Error for quite some time now. It was difficult to find where the error originated from since when I execute the code from a FORM it yields the error yet when I go to debug in VBA Editor with breakpoints the error vanishes.
However I have isolated the problem to the lines:
within the function:
I find it strange that the error comes and goes even though I am opening the same Excel Files.
James
I have been receiving an intermittent "Object Variable or With Block Variable not set" Error for quite some time now. It was difficult to find where the error originated from since when I execute the code from a FORM it yields the error yet when I go to debug in VBA Editor with breakpoints the error vanishes.
However I have isolated the problem to the lines:
Code:
With ActiveSheet.UsedRange
iCol = .Cells(1, 1).Column + .Columns.Count - 1
iLastRow = .Cells(1, 1).Row + .Rows.Count - 1
End With
within the function:
Code:
Function LoadTaskDefInitialisation(xlfilename1)
'On Error GoTo Err_LoadTaskDefInitialisation
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(xlfilename1)
Set oSheet = oBook.Worksheets(1)
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("tblEOTaskDefInitStatus")
iRow = oSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
iCol = oSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
'DoCmd.SetWarnings False
With ActiveSheet.UsedRange
iCol = .Cells(1, 1).Column + .Columns.Count - 1
iLastRow = .Cells(1, 1).Row + .Rows.Count - 1
End With
'DoCmd.SetWarnings True
'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, "EO-") + 1)
''' MsgBox (cTaskCode)
cEONumber = Mid(cTaskCode, 4, 6) ' Extracts only the EO Number from cTaskCode assuming it is 6 numerals long
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 <> "" And cValue <> ("Initialized") Then
rs.AddNew
rs("Task Code") = cTaskCode
rs("EO Number") = cEONumber
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
Exit Function
Err_LoadTaskDefInitialisation:
MsgBox Err.Description, , , , vbExclamation
End Function
I find it strange that the error comes and goes even though I am opening the same Excel Files.
James