excel - MS Access VBA EXCEL.EXE 在 .quit 之后没有终止并且 = 没有
问题描述
我正在运行打开用户提供的电子表格的代码。它只是引入了第一列,不包括标题。运行代码一次按预期工作,除了它使 EXCEL.EXE 实例在最后保持打开状态。我已经阅读了几个有类似问题的问题,所有答案都围绕着找到任何没有退出/关闭然后设置为空的对象。我对代码中的每个对象都执行此操作,甚至进行错误检查,如果它没有完成,则会捕获并退出并清除对象。在 EXCEL.EXE 未关闭后第二次运行代码时,.Cells(Rows.Count, 1).End(xlUp).Row
任何人都知道为什么会抛出“(1004)应用程序定义或对象定义错误”?任何帮助,将不胜感激
Private Sub SplitImports()
Dim StringVar As Variant
Dim strLn As String
'Asks user for Filepath
StringVar = InputBox("Please enter the file path for your list", "Import", "H:\FNMA_WFDC")
'Ends Function if no input or cancel is detected
MsgBox (StringVar)
If (StringVar = vbNullString) Then
MsgBox ("No input, Please try again")
Quittracker = True
Exit Sub
End If
'Scrubs outer quotes if present
StringVar = Replace(StringVar, Chr(34), "", 1, 2)
'Creates the object to check the file
Dim FSO As Object
Set FSO = CreateObject("Scripting.Filesystemobject")
MsgBox ("Got Passed the FSO Object")
'Checks that our file exists, exits if not
If (Not FSO.FileExists(StringVar)) Then
MsgBox ("File does not exist, try again")
Quittracker = True
Exit Sub
End If
Set FSO = Nothing
Dim xlApp As Object 'Excel.Application
Dim xlWrk As Workbook 'Excel.Workbook
Dim i As Long
Set xlApp = New Excel.Application
MsgBox ("Dimmed the excel objects")
xlApp.Visible = False
On Error GoTo ErrorTrap
Set xlWrk = xlApp.Workbooks.Open(StringVar) 'opens the excel file for processing
MsgBox ("objects are set")
With xlWrk.Worksheets("Sheet1")
.Columns("A").NumberFormat = "@"
MsgBox (.Cells(Rows.Count, 1).End(xlUp).Row)
'walks through the excel sheet to the end and inserts the lines below the headerline into the database
For i = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
DoCmd.RunSQL "Insert Into Split_List(Criteria) values('" & .Cells(i, 1).Text & "')"
Next i
End With
MsgBox ("About to Clear and close the objects")
'closes the workbook and quits the application
xlWrk.Saved = True
xlWrk.Close
Set xlWrk = Nothing
xlApp.Quit
Set xlApp = Nothing
MsgBox ("End of the import sub")
Exit Sub
ErrorTrap:
xlWrk.Saved = True
xlWrk.Close
Set xlWrk = Nothing
xlApp.Quit
Set xlApp = Nothing
MsgBox ("(" & Err.Number & ") " & Err.Description
Quittracker = True
Exit Sub
解决方案
推荐阅读
- angular - 具有 ngx-logger 依赖项的 Angular 6 中的单元测试
- android - 来自命令行的 avd 和来自 avd 管理器的 avd 有什么区别?
- vuejs2 - 尝试运行我的 vue js 应用程序时,我的 eslint 出错
- docusignapi - docusign 中的内容类型无效
- content-security-policy - 框架的内容安全策略。框架源与框架祖先
- python - 写入文件的 xml 字符串文字格式错误
- javascript - 交换对象属性 javascript
- java - 如何使用带有插入返回所需的 urlconnection 的 AsyncTask?
- javascript - 为什么 javascript 在浏览器控制台中显示 (66) [empty × 65, {...}]?
- javascript - 获取 Agora.io 语音讨论室中未发布流的用户列表