-
1
- #1
Here is some code that I used on a previous project. I need to modify this to only pull the used range between columns A and AR. Also, I need to import from a specific worksheet named DataEntry. Should I use a named range in my excel workbook? Any help is greatly appreciated. Thx!
Public Sub ImportSeveranceData()
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
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "Excel Files|*.xlsm|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
'Delete the first two rows in the spreadsheet down to Headers
objWorksheet.Range("A1").EntireRow.Delete
'objExcel.Selection.Delete
objWorkbook.Save
'Find last used cell in Column A
Set objCell = .Cells(.Rows.Count, "A").End(xlUp)
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, "tblSeveranceData", _
strXlFileName, True, strWorksheetName & "!" & strUsedRange
End If
MsgBox "Attendance data imported successfully!"
End Sub
Public Sub ImportSeveranceData()
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
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "Excel Files|*.xlsm|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
'Delete the first two rows in the spreadsheet down to Headers
objWorksheet.Range("A1").EntireRow.Delete
'objExcel.Selection.Delete
objWorkbook.Save
'Find last used cell in Column A
Set objCell = .Cells(.Rows.Count, "A").End(xlUp)
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, "tblSeveranceData", _
strXlFileName, True, strWorksheetName & "!" & strUsedRange
End If
MsgBox "Attendance data imported successfully!"
End Sub