每个工作表都是大内存,我发现如果我复制了包含数据的行和列并放入新工作表然后删除旧工作表并重命名新工作表我可以缩小我的工作簿大小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$1")
' 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