Sheet excel 中的大小和内存使用情况

Sheet size and memory usage in excel

每项工作sheet 都在使用大内存,我发现如果我复制其中包含数据的行和列并放入新的 sheet 然后删除旧的并将新的重命名为相同的名称我可以将我的工作簿大小缩小 1 到 3 兆字节,这完全不是内存问题,但当书较小时我的代码似乎 运行 更快。我下载了一个程序,该程序旨在删除所有未使用的单元格,但无法使其正常工作。

    Sub Reset_LastCell()
    ' http://support.microsoft.com/default...&Product=xlw2K
       ' Save the lastcell and start there.
       Dim Sh As Worksheet
    Dim A As Integer

    For Each Sh In Worksheets
    A = A + 1
    If A >= 28 Then
    Sh.Activate
       Set lastcell = Cells.SpecialCells(xlLastCell)
       ' Set the rowstep and column steps so that it can move toward
       ' cell A1.
       rowstep = -1
       colstep = -1
       ' Loop while it can still move.
       While (rowstep + colstep <> 0) And (lastcell.Address <> "$A")
          ' Test to see if the current column has any data in any
          ' cells.
          If Application _
                .CountA(Range(Cells(1, lastcell.Column), lastcell)) _
                > 0 Then colstep = 0  'If data then stop the stepping
             ' Test to see if the current row has any data in any cells.
             ' If data exists, stop row stepping.
             If Application _
                   .CountA(Range(Cells(lastcell.Row, 1), lastcell)) _
                   > 0 Then rowstep = 0
                ' Move the lastcell pointer to a new location.
                Set lastcell = lastcell.Offset(rowstep, colstep)
                ' Update the status bar with the new "actual" last cell
                ' location.
                Application.StatusBar = "Lastcell: " & lastcell.Address
       Wend
       ' Clear and delete the "unused" columns.
       With Range(Cells(1, lastcell.Column + 1), "IV65536")
          Application.StatusBar = "Deleting column range: " & _
             .Address
          .Clear
          .Delete
       End With
       ' Clear and delete the "unused" rows.
       With Rows(lastcell.Row + 1 & ":65536")
          Application.StatusBar = "Deleting Row Range: " & _
             .Address
          .Clear
          .Delete
       End With
       ' Select cell A1.
       ' Reset the status bar to the Microsoft Excel default.
       Application.StatusBar = False
       If A >= 35 Then Exit Sub
       Range("AI2").Select
     End If
     Next

    End Sub

减小工作簿大小的一种非常简单的方法是将文件另存为 .xlsb。所有代码和所有功能继续完全相同地工作。唯一的限制是工作簿不能再被 Libre Office 和其他开源电子表格应用程序打开。

我发现它还有助于加快公式的计算...它可能对代码有帮助。

更新 您的代码在我的机器上运行良好,我唯一需要做的就是将 Dim 添加到代码中,因为我使用 Option Explicit。在顶部的代码中添加以下内容:

Dim lastcell As Range
Dim colstep As Long
Dim rowstep As Long