D
Dale Fye
I've seen other posts and have been working through them to resolve my
problem. The following code (I've cut some of the fluff) works to loop
through sheets of an Excel workbook and import the data into an Access
database. But will not release Excel. Any help would be greatly appreciated.
Public Sub ExcelLitReviewImport(MyFilename)
Dim xlApp As Excel.Application
Dim wbk As Excel.Workbook
Dim sht As Excel.Worksheet
Dim rng As Excel.Range
Dim intSheetNum As Integer, strShtName As String, intRowPointer As Integer
Dim strImportRange As String
Dim qdf As DAO.QueryDef
Dim frm As Form
On Error GoTo ExcelLitReviewImportError
Set frm = Forms("frm_Import_Lit_Review")
CurrentDb.Execute "DELETE * FROM tbl_Temp_Lit_Review", dbFailOnError
'Open Excel and open the workbook to be imported,
frm.lbl_Routine.Visible = True
frm.lbl_Routine.Caption = "Updating Excel file column headers"
DoEvents
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
'Open the workbook
Set wbk = xlApp.Workbooks.Open(Filename:=MyFilename, ReadOnly:=False)
'Loop through all of the numbered sheets
DoCmd.Hourglass True
For intSheetNum = 4 To wbk.Sheets.Count
DoEvents
Set sht = wbk.Sheets(intSheetNum)
sht.Activate
strShtName = sht.Name
'If sheet has data, reformat the header row,
'change the worksheet name, import the worksheet
'then change the worksheet name back
If intRowPointer > 1 Then
'Change the column names to correspond with those in
'tbl_Temp_Lit_Review
sht.Cells(1, 1) = "Task"
sht.Cells(1, 1).Hyperlinks.Delete
sht.Cells(1, 2) = "Sub_Task"
sht.Cells(1, 3) = "Source"
sht.Cells(1, 4) = "Pg_Para"
sht.Cells(1, 5) = "Classification"
sht.Cells(1, 6) = "Potential_Gap"
sht.Cells(1, 7) = "D"
sht.Cells(1, 8) = "O"
sht.Cells(1, 9) = "T"
sht.Cells(1, 10) = "M"
sht.Cells(1, 11) = "L"
sht.Cells(1, 12) = "P"
sht.Cells(1, 13) = "F"
sht.Cells(1, 14) = "P2"
sht.Cells(1, 15) = "Reviewer"
'Remove header row formatting
Set rng = Range("A1:O1")
rng.Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Set rng = Nothing
'Worksheets are name 1.2.1, 1.3.1, ....
'Could not get method to work with periods in worksheet names
'so changed each sheets name, saved it, imported it, then
'changed it back
sht.Name = "ImportThis"
wbk.Save
strImportRange = "ImportThis!A1:O" & intRowPointer
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
"tbl_Temp_Lit_Review", Filename, True,
strImportRange
sht.Name = strShtName
End If
Next
wbk.Save
wbk.Close
xlApp.Quit
End sub
problem. The following code (I've cut some of the fluff) works to loop
through sheets of an Excel workbook and import the data into an Access
database. But will not release Excel. Any help would be greatly appreciated.
Public Sub ExcelLitReviewImport(MyFilename)
Dim xlApp As Excel.Application
Dim wbk As Excel.Workbook
Dim sht As Excel.Worksheet
Dim rng As Excel.Range
Dim intSheetNum As Integer, strShtName As String, intRowPointer As Integer
Dim strImportRange As String
Dim qdf As DAO.QueryDef
Dim frm As Form
On Error GoTo ExcelLitReviewImportError
Set frm = Forms("frm_Import_Lit_Review")
CurrentDb.Execute "DELETE * FROM tbl_Temp_Lit_Review", dbFailOnError
'Open Excel and open the workbook to be imported,
frm.lbl_Routine.Visible = True
frm.lbl_Routine.Caption = "Updating Excel file column headers"
DoEvents
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
'Open the workbook
Set wbk = xlApp.Workbooks.Open(Filename:=MyFilename, ReadOnly:=False)
'Loop through all of the numbered sheets
DoCmd.Hourglass True
For intSheetNum = 4 To wbk.Sheets.Count
DoEvents
Set sht = wbk.Sheets(intSheetNum)
sht.Activate
strShtName = sht.Name
'If sheet has data, reformat the header row,
'change the worksheet name, import the worksheet
'then change the worksheet name back
If intRowPointer > 1 Then
'Change the column names to correspond with those in
'tbl_Temp_Lit_Review
sht.Cells(1, 1) = "Task"
sht.Cells(1, 1).Hyperlinks.Delete
sht.Cells(1, 2) = "Sub_Task"
sht.Cells(1, 3) = "Source"
sht.Cells(1, 4) = "Pg_Para"
sht.Cells(1, 5) = "Classification"
sht.Cells(1, 6) = "Potential_Gap"
sht.Cells(1, 7) = "D"
sht.Cells(1, 8) = "O"
sht.Cells(1, 9) = "T"
sht.Cells(1, 10) = "M"
sht.Cells(1, 11) = "L"
sht.Cells(1, 12) = "P"
sht.Cells(1, 13) = "F"
sht.Cells(1, 14) = "P2"
sht.Cells(1, 15) = "Reviewer"
'Remove header row formatting
Set rng = Range("A1:O1")
rng.Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Set rng = Nothing
'Worksheets are name 1.2.1, 1.3.1, ....
'Could not get method to work with periods in worksheet names
'so changed each sheets name, saved it, imported it, then
'changed it back
sht.Name = "ImportThis"
wbk.Save
strImportRange = "ImportThis!A1:O" & intRowPointer
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
"tbl_Temp_Lit_Review", Filename, True,
strImportRange
sht.Name = strShtName
End If
Next
wbk.Save
wbk.Close
xlApp.Quit
End sub