Okay, I found this code which works but I want to only import the spreadsheet data beginning on row 3 which contains the headers and continue through the used range. I tried using Offset(3,0) but no luck. Also, I would love to use a dialog box to find the file. If anyone could assist, I would greatly appreciate it.
Private Sub Command17_Click()
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
'Path of current Access project
strCurrProjPath = Application.CurrentProject.Path
'Assign Path and filename of XL file to variable
strXlFileName = strCurrProjPath & "\" & "Test.xls"
'Assign Excel application to a variable
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False 'Can be visible or not visible
'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)
End With
'Assign used range to a string variable.
'.Address(0,0) returns A1:F10 type address.
'Absolute address ($A$1:$F$1000) does not work in
'DoCmd.TransferSpreadsheet.....etc.
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:=True
Set objWorkbook = Nothing
objExcel.Quit
Set objExcel = Nothing
'Import the worksheet
DoCmd.TransferSpreadsheet acImport, 8, "tblAttendance_Data", _
strXlFileName, True, strWorksheetName & "!" & strUsedRange
End Sub
Private Sub Command17_Click()
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
'Path of current Access project
strCurrProjPath = Application.CurrentProject.Path
'Assign Path and filename of XL file to variable
strXlFileName = strCurrProjPath & "\" & "Test.xls"
'Assign Excel application to a variable
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False 'Can be visible or not visible
'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)
End With
'Assign used range to a string variable.
'.Address(0,0) returns A1:F10 type address.
'Absolute address ($A$1:$F$1000) does not work in
'DoCmd.TransferSpreadsheet.....etc.
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:=True
Set objWorkbook = Nothing
objExcel.Quit
Set objExcel = Nothing
'Import the worksheet
DoCmd.TransferSpreadsheet acImport, 8, "tblAttendance_Data", _
strXlFileName, True, strWorksheetName & "!" & strUsedRange
End Sub