Process 宏完成后,Excel.exe将保留在内存中
我正在使用Access 2010中的VBA automation来使用Excel 2010创建图表。我的代码工作正常,但使孤立的Excel.exe进程仍在运行。我将问题追溯到在单独的模块中调用Excel VBA函数。如果我取消对该函数的调用,则当代码完成时,Excel进程将关闭。该函数是Chip Pearson的Process 宏完成后,Excel.exe将保留在内存中,process,excel,vba,Process,Excel,Vba,我正在使用Access 2010中的VBA automation来使用Excel 2010创建图表。我的代码工作正常,但使孤立的Excel.exe进程仍在运行。我将问题追溯到在单独的模块中调用Excel VBA函数。如果我取消对该函数的调用,则当代码完成时,Excel进程将关闭。该函数是Chip Pearson的FindAll,经过修改后可以在Access中运行,例如将范围声明为Excel.Range,并将Union方法显式引用为Excel.Application.Union。我需要如何修改此函
FindAll
,经过修改后可以在Access中运行,例如将范围声明为Excel.Range
,并将Union
方法显式引用为Excel.Application.Union
。我需要如何修改此函数,使其不会使孤立的Excel进程运行
以下代码在主过程中绑定Excel应用程序:
Dim oApp As New excel.Application
Dim oBook As excel.Workbook
Dim oSheet As excel.Worksheet
Set oBook = oApp.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
然后,最后:
oBook.Close False
oApp.Quit
Set oSheet = Nothing
Set oBook = Nothing
Set oApp = Nothing
我用这条线给芬德尔打电话:
b = FindAll(oSheet.Range("a2", oSheet.Range("a2").End(xlDown)), strWellID).Rows.Count
FindAll
功能如下:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' modFindAll
' By Chip Pearson, chip@cpearson.com. www.cpearson.com
' 24-October-2007
' This module is described at www.cpearson.com/Excel/FindAll.aspx
' Requires Excel 2000 or later.
'
' This module contains two functions, FindAll and FindAllOnWorksheets that are use
' to find values on a worksheet or multiple worksheets.
'
' FindAll searches a range and returns a range containing the cells in which the
' searched for text was found. If the string was not found, it returns Nothing.
' FindAllOnWorksheets searches the same range on one or more workshets. It return
' an array of ranges, each of which is the range on that worksheet in which the
' value was found. If the value was not found on a worksheet, that worksheet's
' element in the returned array will be Nothing.
'
' In both functions, the parameters that control the search have the same meaning
' and effect as they do in the Range.Find method.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function FindAll(SearchRange As Excel.Range, _
FindWhat As Variant, _
Optional LookIn As XlFindLookIn = xlValues, _
Optional LookAt As XlLookAt = xlWhole, _
Optional SearchOrder As XlSearchOrder = xlByRows, _
Optional MatchCase As Boolean = False) As Range
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' FindAll
' This searches the range specified by SearchRange and returns a Range object
' that contains all the cells in which FindWhat was found. The search parameters to
' this function have the same meaning and effect as they do with the
' Range.Find method. If the value was not found, the function return Nothing.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim FoundCell As Excel.Range
Dim FirstFound As Excel.Range
Dim LastCell As Excel.Range
Dim ResultRange As Excel.Range
With SearchRange
Set LastCell = .Cells(.Cells.Count)
End With
'On Error Resume Next
On Error GoTo 0
Set FoundCell = SearchRange.Find(What:=FindWhat, _
after:=LastCell, _
LookIn:=LookIn, _
LookAt:=LookAt, _
SearchOrder:=SearchOrder, _
MatchCase:=MatchCase)
If Not FoundCell Is Nothing Then
Set FirstFound = FoundCell
Set ResultRange = FoundCell
Set FoundCell = SearchRange.FindNext(after:=FoundCell)
Do Until False ' Loop forever. We'll "Exit Do" when necessary.
If (FoundCell Is Nothing) Then
Exit Do
End If
If (FoundCell.Address = FirstFound.Address) Then
Exit Do
End If
Set ResultRange = Excel.Application.Union(ResultRange, FoundCell)
Set FoundCell = SearchRange.FindNext(after:=FoundCell)
Loop
End If
Set FindAll = ResultRange
'added below
Set ResultRange = Nothing
Set FoundCell = Nothing
Set FirstFound = Nothing
Set LastCell = Nothing
Set SearchRange = Nothing
End Function
Jimmy Pena和Siddharth Rout就如何帮助删除幻影excel流程提供了极好的意见 我只想补充一点,虽然我不一定在所有情况下都推荐它,但如果它是一个持续存在的问题,并且您在排除故障时正在寻找一个快速的“解决方案”,您可以强行关闭,如本问题中所述: 测试表明,这一过程即使不是特别优雅,也会迅速结束 您还可以使用迭代循环以稍微优雅的方式结束这些过程,但仍然使用系统过程方法,而不是应用程序/应用程序接口方法:
虽然我不赞成将这些方法中的任何一种方法作为一种总体解决方案,以避免由于错误检查不完整或不充分、代码封装或常规回调、方法使用、变量实例化或其他编程问题而使实例处于活动状态,在需要修复的情况下,这是一个修复。Jimmy Pena和Siddharth Rout就如何帮助删除phantom excel流程提供了极好的意见 我只想补充一点,虽然我不一定在所有情况下都推荐它,但如果它是一个持续存在的问题,并且您在排除故障时正在寻找一个快速的“解决方案”,您可以强行关闭,如本问题中所述: 测试表明,这一过程即使不是特别优雅,也会迅速结束 您还可以使用迭代循环以稍微优雅的方式结束这些过程,但仍然使用系统过程方法,而不是应用程序/应用程序接口方法:
虽然我不赞成将这些方法中的任何一种方法作为一种总体解决方案,以避免由于错误检查不完整或不充分、代码封装或常规回调、方法使用、变量实例化或其他编程问题而使实例处于活动状态,在需要修复的情况下,它是一个修复。在将
SearchRange
传递给上述函数之前,您如何绑定Excel应用程序?Excel留下实例的原因是因为您没有冲厕运行上述函数后,您必须退出Excel应用程序并将其设置为nothing。oBook.Close False oApp.Quit Set oSheet=Nothing Set oBook=Nothing Set oApp=Nothing尝试此操作。将Excel.Application.Union(ResultRange,FoundCell)
替换为oApp.Union(ResultRange,FoundCell)
,然后重试……这会导致运行时错误424“需要对象”当它到达联合行时。@SiddharthRout在运行上述函数后,您必须退出Excel应用程序并将其设置为“无”
当代码具有尺寸时,这怎么可能呢。。。是否为新的
oApp
将继续重新实例化。在将SearchRange
传递给上述函数之前,您如何绑定Excel应用程序?Excel留下实例的原因是因为您没有冲厕运行上述函数后,您必须退出Excel应用程序并将其设置为nothing。oBook.Close False oApp.Quit Set oSheet=Nothing Set oBook=Nothing Set oApp=Nothing尝试此操作。将Excel.Application.Union(ResultRange,FoundCell)
替换为oApp.Union(ResultRange,FoundCell)
,然后重试……这会导致运行时错误424“需要对象”当它到达联合行时。@SiddharthRout在运行上述函数后,您必须退出Excel应用程序并将其设置为“无”
当代码具有尺寸时,这怎么可能呢。。。是否为新的
<代码>oApp将继续重新启动。
Sub ForceExcelExit()
Dim BruteForce As String
BruteForce = "TASKKILL /F /IM excel.exe"
Shell BruteForce, vbHide
End Sub