在用Office软件Excel时,经常遇到这样一个问题,明明Excel文件中没几行数据,但整个Excel文件大小达数MB,甚至是几十MB大小,Excel打开相当吃力,完全无法操作。有没有办法解决Excel文件内容很少,但文件很大,打开很慢、很卡这一怪象呢?
以前遇到过这样的问题,今天又遇到了,一个就一个小表的Excel文件居然有2.4MB,打开非常缓慢,输入几个字卡得十分严重。好在之前已经有解决方案,废话不多说,直接方法。
在Excel工具栏中依次点开“开发工具”、“查看源代码”,如下图:
什么?你的Excel没有这个功能?好吧,功能肯定是有的,只是你没打开而已。自行百度打开方式,或者查看《Excel表格中没有开发者工具?怎么办?》
下面,在开发工具中输入如下代码:Sub test()
Dim count
Dim pic '图片13
Dim button '按钮8
Dim txtbox '文本框17
Dim comm '批注4
Dim other '其他未知
Dim msg '提示消息
Dim delpic
count = 0
pic = 0
button = 0
txtbox = 0
comm = 0
other = 0
respons = MsgBox("是否要清理表格中的图片,请谨慎操作!" & Chr(10) & _
"点击'是'清理图片,点击'否'跳过!", vbYesNo, "警告")
If respons = vbYes Then
delpic = True
Else
delpic = False
End If
For i = 1 To Sheets.count
For Each tb In Sheets(i).Shapes
If tb.Type = 13 Then
pic = pic + 1
If delpic Then
tb.Delete
End If
ElseIf tb.Type = 8 Then
button = button + 1
tb.Delete
ElseIf tb.Type = 17 Then
txtbox = txtbox + 1
tb.Delete
ElseIf tb.Type = 4 Then
comm = comm + 1
Else
other = other + 1
tb.Delete
End If
Next
'For Each tb In ActiveSheet.Shapes
'tb.Delete
'Next
Next
If delpic Then
count = button + txtbox + pic + other
Else
count = button + txtbox + other
End If
If count > 0 Or comm > 0 Or pic > 0 Then
msg = "共删除了" & count & "个对象;"
If button > 0 Then
msg = msg & Chr(10) & "按钮" & count & "个;"
ElseIf txtbox > 0 Then
msg = msg & Chr(10) & "文本框" & txtbox & "个;"
ElseIf pic > 0 And delpic Then
msg = msg & Chr(10) & "图片" & pic & "个;"
ElseIf other > 0 Then
msg = msg & Chr(10) & "未知对象" & other & "个;"
End If
If comm > 0 Then
msg = msg & Chr(10) & "有" & comm & "个批注没有处理;"
End If
If pic > 0 And Not delpic Then
msg = msg & Chr(10) & "有" & pic & "个图片没有处理;"
End If
Else
msg = "没有找到可以清理的对象!"
End If
MsgBox msg
End Sub
再点运行按钮,如下图:
经过漫长的等待后,运行完成,删掉输入的代码,保存Excel,看看是不是Excel文件变小了,同时打开也变快了呢?
经上面的一顿操作,其主要作用是删除Excel文件中的隐藏对象,这些隐藏对象,我们肉眼看不到,但非常多,几十万、上百万个都有可能,导致Excel本身很小但占用很大,计算机处理起来困难,这就非常卡了。形成隐藏对象的原因大多是复制粘贴导致的,具体原因本文不作讨论。
OK,问题得以解决。