excel - 为数组分配范围时出现 VBA 内存问题

标签 excel vba memory range memory-leaks

我的目的是为一个 Excel 单元格区域分配一个二维变体数组的值。

分配工作正常,但我遇到内存问题(泄漏):当我启动 Excel 时,该过程大约需要 22 Mo 的 Ram。运行以下代码(创建新工作簿、分配范围、关闭工作簿而不保存)后,该过程占用 33 Mo RAM。

有谁知道我做错了什么吗?

问候, 亚历山大

这里是代码

Option Explicit

Sub test()
   Dim nLines As Long
   Dim xSize As Long
   Dim j As Long
   Dim i As Long

   'Creating New Empty Workbook
   Application.Workbooks.Add
   Range("A1").Select

   Application.ScreenUpdating = False

   nLines = 10000
   xSize = 200

   Dim myRange As Range
   Dim myArray() As Variant

   ReDim myArray(1 To nLines, 1 To xSize)

   'Assigning some values
   For j = 1 To nLines:
       For i = 1 To xSize:
        myArray(j, i) = i * 4 / 3#
       Next i
   Next j

   Set myRange = Range(ActiveCell, ActiveCell.Offset(nLines - 1, xSize - 1))
   myRange .Value = myArray

   'Cleaning up
    Erase myArray
    Set myRange = Nothing

    'Closing workbook without saving
    Application.ActiveWorkbook.Close (False)

    Application.ScreenUpdating = True

End Sub

最佳答案

VBA 不是 C,不要指望能够以任何方式控制 Excel 的内存占用。 Excel 本身并不擅长内存管理,而且通常情况下,您正在运行宏或您在这些宏中所做的事情几乎无法控制 Excel 决定分配多少内存,或者它是否曾经决定再次释放它。

话虽如此,您可以在代码中做一些事情来尝试减少它的困惑。

  1. Range("A1").Select

    It's generally bad practice to select cells before operating on them in Excel. The only time you need to select a cell is if you are illustrating something to the viewer of the macro. Furthermore, this code can actually cause a problem in certain versions of excel since you just added a workbook and it there's nothing to tell excel what workbook, and what worksheet from that workbook it should be selecting. You should remove this code altogether (it doesn't impact your logic anyway).

  2. 设置 myRange = Range( ...

    This code isn't necessary since you're only performing one action with the range. You don't need to create the myRange variable at all, so you also don't have to worry about setting it to nothing, or more importantly - the fact that setting it to nothing doesn't actually accomplish anything memory-wise.

  3. ActiveCellApplication.ActiveWorkbook

    Again, these unqualified references based on calls to 'Select' and the 'ActiveWorkbook' aren't explicit, and can cause unreliable behavior, especially if the user has other spreadsheets open in the same instance of excel. You're better off storing a reference to the workbook you added in the beginning.

总而言之,我会将您的代码更改为:

Sub test()
    Const nLines As Long = 10000, xSize As Long = 200
    Dim i As Long, j As Long
    Dim newWorkbook As Workbook
    
    Application.ScreenUpdating = False
    
    'Creating New Empty Workbook
    Set newWorkbook = Application.Workbooks.Add
    
    'Assigning some values
    Dim myArray(1 To nLines, 1 To xSize) As Double
    For j = 1 To nLines:
       For i = 1 To xSize:
        myArray(j, i) = i * 4 / 3#
       Next i
    Next j
    
    With newWorkbook.Sheets(1)
        .Range(.Cells(1, 1), .Cells(nLines - 1, xSize - 1)).Value = myArray
    End With
    
    'Closing workbook without saving
    newWorkbook.Close (False)
    
    'Cleaning up
    Erase myArray
    Set newWorkbook = Nothing
        
    Application.ScreenUpdating = True
End Sub

结束语:

除了 VBA 编码类(class) - 请注意,打开新工作簿的行为将要求 Excel 分配大量新内存 - 比创建临时数组的方式更多。即使在您关闭新工作簿之后,这也是 excel 可以释放或保留的内存。将无法回避这些类型的增加。请注意,Excel 的内存占用不仅仅与您当前的工作簿相关,它与实例(支持多个工作簿)的整个生命周期及其备份、优化的计算树、撤消历史记录等相关。这真的没有意义将新启动的 excel 实例的占用空间与刚刚完成的“一堆东西”的占用空间进行比较。

另请注意,仅仅因为 Excel 没有决定释放该内存,它不会导致内存泄漏。 Excel 可能已经在可回收/可重复使用的对象中分配了内存,这些对象将被您决定打开的任何下一个工作簿重新填充。反复重新运行代码可能每次都会导致内存小幅增加,但 Excel 也可以跟踪一定数量的实例操作历史记录。

为了(有趣地)说明我的观点,下面是 Excel (2007) 在打开一个新实例并运行您的代码一次、9 次,然后再运行 40 次时的内存占用。

enter image description here

这不是您的代码的错,您也没有责任为此担心,如果您的业务用户担心,他们不应该选择旧版本的 Excel 作为他们的目标开发平台。

关于excel - 为数组分配范围时出现 VBA 内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10381834/

相关文章:

excel - 删除工作表的自动化错误

vba - Word 宏 - 从包含特定文本的所有链接中删除超链接

excel - 从工作簿上的说明表创建工作簿按钮

java - 类class[B在Java中代表什么?

hibernate - hibernate 缓存问题

node.js - 为什么空对象比非空对象占用更多内存?

excel - VBA 查找值并在消息框中报告并更改同一行单元格中的值

excel - SAS导出到Excel,无法读回SAS

vba - 函数始终返回 bool 值 False

excel - 使用VBA删除Excel中的空行