每个工作表都需要大量内存,我发现如果我将其中包含数据的行和列复制到新表中,然后删除旧表并重命名新表,可以缩小工作簿的大小增加了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
答案 0 :(得分:0)
减小工作簿大小的一种非常简单的方法是将文件另存为.xlsb
。所有代码和所有功能继续完全相同地工作。唯一的限制是该工作簿无法再由Libre Office和其他开源电子表格应用程序打开。
我发现它还有助于加快公式的计算速度……可能有助于编写代码。
更新
您的代码在我的机器上可以正常工作,我唯一需要做的就是将Dim
添加到代码中,因为我使用的是Option Explicit
。将以下内容添加到顶部的代码中:
Dim lastcell As Range
Dim colstep As Long
Dim rowstep As Long