A
AdamY
Hi all,
I have tried so many different ways of coding this I can't even count them
anymore. No matter what, there is always an instance of EXCEL.exe running in
my task manager processes. This instance not closing makes the application
not able to run more than one time, unless EXCEL.exe is manually terminated.
My code is below. If I comment out the TransferSpreadsheet line it closes
just fine. I have thread threads on tons of sites on this and no one's
suggestions work. I am very frustrated, any help would be greatly
appreciated! Thanks...
Public Sub ImportData(ByVal strBook As String)
'create variables
Dim objExcel As Excel.Application
Dim objBook As Excel.Workbook
Dim strSheet, strEdit, strX As String
Dim intFind, intFind2, x, intRows, y As Integer
Dim bolCheck As Boolean
Set objExcel = CreateObject("Excel.Application")
Set objBook = objExcel.Workbooks.Open(strBook)
'loop through all worksheets to get data
For x = 1 To objBook.Worksheets.Count
strEdit = objBook.Worksheets(x).Range("I2")
bolCheck = strEdit Like "Select*"
If (bolCheck = True) Then
intFind2 = InStr(1, strEdit, "E", vbBinaryCompare)
strEdit = Mid(strEdit, intFind2)
intFind = InStr(1, strEdit, " ")
strEdit = Left(strEdit, intFind - 1)
strX = CStr(x)
'set worksheet name, sheet variable, and column headers
objBook.Worksheets(x).Name = strEdit & strX
strSheet = strEdit & strX & "!"
objBook.Worksheets(x).Range("J1").Value = "Table Name"
objBook.Worksheets(x).Range("K1").Value = "Workbook Name"
'count number of populated rows in sheet
intRows = 2
Do Until objBook.Worksheets(x).Cells(intRows, 1).Value = ""
intRows = intRows + 1
Loop
intRows = intRows - 1
'populate worksheet/table name field
For y = 2 To intRows
objBook.Worksheets(x).Cells(y, 10).Value = strEdit
Next y
'populate workbook field
For y = 2 To intRows
objBook.Worksheets(x).Cells(y, 11).Value = strBook
Next y
'pull spreadsheet data into access table
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
"Members Table", strBook, True, strSheet
End If
Next x
objBook.Close False
Set objBook = Nothing
With objExcel.Workbooks
Do While .Count > 0
.Item(.Count).Close False
Loop
End With
objExcel.Quit
Set objExcel = Nothing
End Sub
I have tried so many different ways of coding this I can't even count them
anymore. No matter what, there is always an instance of EXCEL.exe running in
my task manager processes. This instance not closing makes the application
not able to run more than one time, unless EXCEL.exe is manually terminated.
My code is below. If I comment out the TransferSpreadsheet line it closes
just fine. I have thread threads on tons of sites on this and no one's
suggestions work. I am very frustrated, any help would be greatly
appreciated! Thanks...
Public Sub ImportData(ByVal strBook As String)
'create variables
Dim objExcel As Excel.Application
Dim objBook As Excel.Workbook
Dim strSheet, strEdit, strX As String
Dim intFind, intFind2, x, intRows, y As Integer
Dim bolCheck As Boolean
Set objExcel = CreateObject("Excel.Application")
Set objBook = objExcel.Workbooks.Open(strBook)
'loop through all worksheets to get data
For x = 1 To objBook.Worksheets.Count
strEdit = objBook.Worksheets(x).Range("I2")
bolCheck = strEdit Like "Select*"
If (bolCheck = True) Then
intFind2 = InStr(1, strEdit, "E", vbBinaryCompare)
strEdit = Mid(strEdit, intFind2)
intFind = InStr(1, strEdit, " ")
strEdit = Left(strEdit, intFind - 1)
strX = CStr(x)
'set worksheet name, sheet variable, and column headers
objBook.Worksheets(x).Name = strEdit & strX
strSheet = strEdit & strX & "!"
objBook.Worksheets(x).Range("J1").Value = "Table Name"
objBook.Worksheets(x).Range("K1").Value = "Workbook Name"
'count number of populated rows in sheet
intRows = 2
Do Until objBook.Worksheets(x).Cells(intRows, 1).Value = ""
intRows = intRows + 1
Loop
intRows = intRows - 1
'populate worksheet/table name field
For y = 2 To intRows
objBook.Worksheets(x).Cells(y, 10).Value = strEdit
Next y
'populate workbook field
For y = 2 To intRows
objBook.Worksheets(x).Cells(y, 11).Value = strBook
Next y
'pull spreadsheet data into access table
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
"Members Table", strBook, True, strSheet
End If
Next x
objBook.Close False
Set objBook = Nothing
With objExcel.Workbooks
Do While .Count > 0
.Item(.Count).Close False
Loop
End With
objExcel.Quit
Set objExcel = Nothing
End Sub