我已经做了2个函数“ WrongEntries”和“ Duplicates”。当我单独运行它时,它运行完美。但是,当我一个接一个地运行它时,它反映了对象“ _global”的方法“范围”失败。
我不确定是不是因为对象仍在上一个运行函数的内存中,但是我确实在两个函数中使用了不同的对象和变量,但仍然存在相同的错误。还尝试将对象视为空。
断章取义的部分是,如果我洗牌,他们的运行优先总是始终完美运行,则不管是哪一个。
Function WrongEntries()
Dim dbs As Database Dim lRow As Long Dim lCol As Long Dim rg As Range
Set dbs = CurrentDb
Set rgQuery = dbs.OpenRecordset("5G High Cycle Times")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.Workbooks.Open("\5G Wrong
Entries_Blank.xlsx")
targetWorkbook.Worksheets("Cycle Times >5
weeks").Range("A2").CopyFromRecordset rgQuery
targetWorkbook.Worksheets("Cycle Times >5 weeks").Activate
lRow = Range("b" & Rows.Count).End(xlUp).Row
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
excelApp.Quit
Set excelApp = Nothing
End Function
'''问题是当我与第一个一起运行第二个时”
Function Duplicates()
Dim dbs As Database Dim lastRw As Long Dim lastCl As Long Dim rnge As
Range Dim wks As Worksheet
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set dbs = CurrentDb
Set rdQuery = dbs.OpenRecordset("5G Duplicates Check")
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.Workbooks.Open("\5G Duplicates_Blank.xlsx")
Set wks = targetWorkbook.Worksheets("Duplicates")
targetWorkbook.Worksheets("Duplicates").Range("A2").CopyFromRecordset
rdQuery
lastRw = wks.Range("a" & Rows.Count).End(xlUp).Row
lastCl = wks.Cells(1, Columns.Count).End(xlToLeft).Column
End Function
答案 0 :(得分:2)
您关闭该应用程序:
excelApp.Quit
Set excelApp = Nothing
离开工作簿和所有孤立的对象。所以:
targetWorkbook.Close
Set targetWorkbook = Nothing
excelApp.Quit
Set excelApp = Nothing
进一步:始终使用特定的对象。不是
Range("b" & Rows.Count).End(xlUp).Row
Cells(1, Columns.Count).End(xlToLeft).Column
但是:
Set range = SomeWorksheet.Range ...
set cell = SomeWorksheet.Cells ...
,并先终止它们:
Set cell = Nothing
Set range = Nothing
Set wks = nothing
targetWorkbook.Close
Set targetWorkbook = Nothing
excelApp.Quit
Set excelApp = Nothing