VBA Excel:存储在内存中的局部变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18382310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:54:23  来源:igfitidea点击:

VBA Excel: Local variables stored in memory?

excelvba

提问by karamell

In VBA Excel,

在 VBA Excel 中,

I've heard that it's good to declare variables, that are of no further use, as Nothingin the end of module to save memory.

我听说最好Nothing在模块末尾声明不再使用的变量以节省内存。

Example:

例子:

Dim myRange As Range

Sub Main

   set myRange = Sheets("Sheet1").range("A1:H20")
   ' Do Something
   ' ...
   set myRange = Nothing

End Sub

Is there any truth to this statement or does VBA Excel handle this issue automatically?

此声明是否属实,或者 VBA Excel 是否会自动处理此问题?

回答by enderland

Variables declared within the scope of your procedure are destroyed correctly afterwards.

在您的程序范围内声明的变量随后会被正确销毁。

So in your example, because myRangeis defined within the scope of Mainit is destroyed/cleaned up appropriately.

所以在你的例子中,因为myRangeMain它的范围内定义被适当地销毁/清理。

However, if you had a module with this code:

但是,如果您有一个包含此代码的模块:

Public testForStackOverflow As Range

Sub main()
    Call mySubSub
    MsgBox testForStackOverflow.Address

    Call mySubDestroyer
    If (testForStackOverflow Is Nothing) Then
        MsgBox "destroyed"
    End If

    Call mySubSub
End Sub

Sub mySubSub()
    Set testForStackOverflow = Range("A1")
End Sub
Sub mySubDestroyer()
    Set testForStackOverflow = Nothing
End Sub

Sub callAfterRunningMain()

    MsgBox testForStackOverflow.Address
End Sub

the global is not automatically cleaned up after mySubSub(as should be expected).

全局不会在之后自动清理mySubSub(正如预期的那样)。

What you might notexpect is that if you run callAfterRunningMainafter running the mainmethod, your global still has a value. This global is not cleaned up unless you manually do it, close Excel, or use Endsomewhere in your code.

您可能没有想到的是,如果您callAfterRunningMain在运行该main方法后运行,您的全局变量仍然具有值。除非您手动执行、关闭 Excel 或End在代码中的某处使用,否则不会清除此全局变量。

So if you heavily used global variables and did not clean them up somehow, you will keep them in memory indefinitely. This is the case with userforms and using UserForm.hidevs Unload Metoo, by the way.

因此,如果您大量使用全局变量并且没有以某种方式清理它们,您将无限期地将它们保留在内存中。顺便说一下,这就是用户表单和使用UserForm.hidevsUnload Me的情况。



The related questions don't really address what I describe and it can be quite confusing. Especially when trying to "reset" UserForms.

相关问题并没有真正解决我所描述的问题,而且可能会令人困惑。特别是在尝试“重置”用户窗体时。

回答by Stewbob

If you are dealing with a large number of object variables, like a `Range', at one time, or in a loop that re-creates them before disposing of old objects, Excel will have a problem with that. Garbage collection occurs when Excel thinks it should; some indeterminate time after a variable goes out of scope.

如果您一次处理大量对象变量,例如“范围”,或者在处理旧对象之前重新创建它们的循环中,Excel 将遇到问题。垃圾收集发生在 Excel 认为应该发生的时候;变量超出范围后的一些不确定时间。

If you are doing lots of repetitions of object variable creation, without leaving the scope in which they are created, it is a verygood idea to dispose of them (..=Nothing) when you are done with them.

如果您要多次重复创建对象变量,而不离开创建它们的范围,那么在完成它们后将它们处理掉 ( )是一个非常好的主意..=Nothing

回答by Tarik

Even if you do not do that, the fact that the variable goes out of scope will have the any un-referenced object garbage collected.

即使您不这样做,变量超出范围的事实也会使任何未引用的对象垃圾收集。