简体   繁体   English

错误:- 对象“_global”的方法“range”在同时运行 2 个函数时失败

[英]Error:- Method 'range' of object '_global' failed while running 2 functions together

I have made 2 functions "WrongEntries" & "Duplicates".我做了 2 个函数“WrongEntries”和“Duplicates”。 When I run it individually it runs perfectly.当我单独运行它时,它运行完美。 But when I run it one after the other it reflects Method 'range' of object '_global' failed .但是当我一个接一个地运行它时,它反映了对象 '_global' failed 的 Method 'range'

I am not sure is it because of the objects still in the memory of the previous run function but i did use different objects & variable in both functions still same error was there.我不确定是不是因为对象仍在前一个运行函数的内存中,但我确实在两个函数中使用了不同的对象和变量,但仍然存在相同的错误。 Also tried putting objects as nothing.还尝试将对象作为无。

Stange part is if I shuffle their run-first one always runs perfectly doesn't matter which one it is.奇怪的部分是,如果我洗牌他们的第一个运行总是完美的并不重要它是哪个。

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

''' Problems is when I run second one together with first''' '''问题是当我和第一个一起运行第二个时'''

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

Here the error is reflecting while calculating last row count这里在计算最后一行计数时反映了错误

lastRw = wks.Range("a" & Rows.Count).End(xlUp).Row
lastCl = wks.Cells(1, Columns.Count).End(xlToLeft).Column
End Function

You close the application:您关闭应用程序:

excelApp.Quit
Set excelApp = Nothing

leaving the workbook and all objects orphaned.离开工作簿和所有对象孤立。 So:所以:

targetWorkbook.Close
Set targetWorkbook = Nothing
excelApp.Quit
Set excelApp = Nothing

Further: Always use specific objects.进一步:始终使用特定对象。 Not不是

Range("b" & Rows.Count).End(xlUp).Row
Cells(1, Columns.Count).End(xlToLeft).Column

but:但:

Set range = SomeWorksheet.Range ...
set cell = SomeWorksheet.Cells ...

and terminate these as the first:并将这些作为第一个终止:

Set cell = Nothing
Set range = Nothing
Set wks = nothing
targetWorkbook.Close
Set targetWorkbook = Nothing
excelApp.Quit
Set excelApp = Nothing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM