This used to work in Access 2003, but I am getting an object defined error in Access 2007 on this code:
Set objCell = .Cells(.Rows.Count, "A").End(xlUp).Row
What am I doing wrong?
Thx,
Dim strCurrProjPath As String
Dim objExcel As Object 'Excel.Application
Dim objWorkbook As Object 'Excel.Workbook
Dim objWorksheet As Object 'Worksheet
Dim strXlFileName As String 'Excel Workbook name
Dim strWorksheetName As String 'Excel Worksheet name
Dim objCell As Object 'Last used cell in column
Dim strUsedRange As String 'Used range
Dim FileName As String
Dim objDialog, boolResult
'On Error GoTo Err_ImportEmployeeData
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "Excel Files|*.xlsx; *.xls|All Files|*.*"
objDialog.FilterIndex = 1
boolResult = objDialog.ShowOpen
If boolResult = 0 Then
Exit Sub
Else
'Assign Path and filename of XL file to variable
strXlFileName = objDialog.FileName
'Assign Excel application to a variable
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False 'Can be visible or not visible
objExcel.UserControl = True
'Open the Excel Workbook
Set objWorkbook = objExcel.Workbooks.Open(strXlFileName)
'Assign required worksheet to a variable
With objWorkbook
Set objWorksheet = .Worksheets(1)
End With
With objWorksheet
'Assign worksheet name to a string variable
strWorksheetName = .Name
'Find last used cell in Column A
Set objCell = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'Assign used range to a string variable.
strUsedRange = objWorksheet.UsedRange.Address(0, 0)
'Turn off/Close in reverse order to setting/opening.
Set objCell = Nothing
Set objWorksheet = Nothing
'SaveChanges = False suppresses save message
objWorkbook.Close SaveChanges:=False
Set objWorkbook = Nothing
objExcel.Quit
Set objExcel = Nothing
'Import the worksheet
DoCmd.TransferSpreadsheet acImport, 8, "tblEmployee", _
strXlFileName, True, strWorksheetName & "!" & strUsedRange
End If
MsgBox "Employee data imported successfully!"
Exit_ImportEmployeeData:
Exit Sub
Err_ImportEmployeeData:
MsgBox Err.Description
Resume Exit_ImportEmployeeData
End Sub