跳到主要内容

如何检查工作簿的每个工作表的大小?

假设您有一个包含多个工作表的大型工作簿,现在,您想找出每个工作表的大小,以确定需要减少的工作表。 有没有快速的方法来处理此任务?

使用VBA代码检查每个工作表的大小

使用 Kutools for Excel 检查每个工作表的大小


箭头蓝色右气泡 使用VBA代码检查每个工作表的大小

使用以下VBA代码,您可以快速获取工作簿中每个工作表的大小。 请这样做:

1。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic for Applications窗口.

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块窗口.

VBA代码:检查工作簿中每个工作表的大小

Sub WorksheetSizes()
'Update 20140526
Dim xWs As Worksheet
Dim Rng As Range
Dim xOutWs As Worksheet
Dim xOutFile As String
Dim xOutName As String
xOutName = "KutoolsforExcel"
xOutFile = ThisWorkbook.Path & "\TempWb.xls"
On Error Resume Next
Application.DisplayAlerts = False
Err = 0
Set xOutWs = Application.Worksheets(xOutName)
If Err = 0 Then
    xOutWs.Delete
    Err = 0
End If
With Application.ActiveWorkbook.Worksheets.Add(Before:=Application.Worksheets(1))
    .Name = xOutName
    .Range("A1").Resize(1, 2).Value = Array("Worksheet Name", "Size")
End With
Set xOutWs = Application.Worksheets(xOutName)
Application.ScreenUpdating = False
xIndex = 1
For Each xWs In Application.ActiveWorkbook.Worksheets
    If xWs.Name <> xOutName Then
        xWs.Copy
        Application.ActiveWorkbook.SaveAs xOutFile
        Application.ActiveWorkbook.Close SaveChanges:=False
        Set Rng = xOutWs.Range("A1").Offset(xIndex, 0)
        Rng.Resize(1, 2).Value = Array(xWs.Name, VBA.FileLen(xOutFile))
        Kill xOutFile
        xIndex = xIndex + 1
    End If
Next
Application.ScreenUpdating = True
Application.Application.DisplayAlerts = True
End Sub

3. 然后按 F5 执行此代码的键,以及名为 KutoolsForexcel. 将插入到包含每个工作表名称和文件大小的当前工作簿中,单位为“位”。 看截图:

文档检查表大小 1


箭头蓝色右气泡 使用 Kutools for Excel 检查每个工作表的大小

如果你有 Kutools for Excel,其 拆分工作簿 实用程序,您可以将整个工作簿拆分为单独的文件,然后转到特定的文件夹以检查每个文件的大小。

Kutools for Excel 包括300多个便捷的Excel工具。 30天免费试用,不受限制。 立即获取.

安装 Kutools for Excel 后,请执行以下步骤:

1。 打开您要检查其每个工作表的大小的工作簿,然后单击 企业 > 工作簿工具 > 拆分工作簿,请参见屏幕截图:

文档检查表大小 1

2。 在 拆分工作簿 对话框,检查所有工作表,然后单击 分裂 按钮,然后指定一个文件夹来放置新的工作簿文件。 查看屏幕截图:

文档检查表大小 3
-1
文档检查表大小 4

3。 然后,当前工作簿的每个工作表将另存为单独的Excel文件,您可以转到您的特定文件夹以检查每个工作簿的大小。

文档检查表大小 1

要了解有关此拆分工作簿功能的更多信息。


相关文章:

如何在Excel中拆分工作簿以分隔Excel文件?

如何在Excel中将工作表和工作表导出并保存为新工作簿?

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (9)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Should probably add this between lines 9 and 10 in case some sheets are hidden to avoid the code crashing
For Each xWs In Sheets: xWs.Visible = True: Next
This comment was minimized by the moderator on the site
Thank you, very helpful,
I had a bunch of unnecessary formulas in a sheet and i just deleted that the file now works fine.
All these happen only because i could find the size of each sheet,
Thanks again.

Anson
This comment was minimized by the moderator on the site
Very VeryVeryVeryVery helpful.
Thank you!!
This comment was minimized by the moderator on the site
' Part 3 of 3 '--- paste break --- ' Format the output sheet Application.Sheets(xOutName).Activate Columns("B:B").Select Selection.NumberFormat = "#,##0_);(#,##0)" Columns("A:B").Select Columns("A:B").EntireColumn.AutoFit Range("A1").Select ' Even better, format it as a table. ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1:B" & xIndex), , xlYes).Name = "WorksheetSizes" Application.ScreenUpdating = True Application.Application.DisplayAlerts = True Application.StatusBar = "" Application.Cursor = xlDefault Exit Sub ErrorHandler: MsgBox "Error #" & Err.Number & " - " & Err.Description & vbCrLf & "in procedure WorksheetSizes" End Sub
This comment was minimized by the moderator on the site
' Part 2 of 3 '--- paste break --- xWs.Visible = xlSheetVisible ' xOutFile = ThisWorkbook.Path & "\" & xWs.Name & ".xls" xWs.CopyQ Application.ActiveWorkbook.SaveAs xOutFile Application.ActiveWorkbook.Close SaveChanges:=False Set rng = xOutWs.Range("A1").Offset(xIndex, 0) rng.Resize(1, 2).Value = Array(xWs.Name, VBA.FileLen(xOutFile)) Kill xOutFile xIndex = xIndex + 1 End If Next ' Repeat the above for chart sheets. For Each xWs In Application.ActiveWorkbook.Charts If xWs.Name xOutName Then Application.StatusBar = "Calculating Worksheet Sizes, Sheet " & xIndex & " of " & ActiveWorkbook.Worksheets.count - 1 & " - " & xWs.Name Debug.Print "Calculating Worksheet Sizes, Sheet " & xIndex & " of " & ActiveWorkbook.Worksheets.count - 1 & " - " & xWs.Name DoEvents ' include this so CTRL+Break can be detected. xWs.Visible = xlSheetVisible xOutFile = ThisWorkbook.Path & "\" & xWs.Name & ".xls" xWs.Copy Application.ActiveWorkbook.SaveAs xOutFile Application.ActiveWorkbook.Close SaveChanges:=False Set rng = xOutWs.Range("A1").Offset(xIndex, 0) rng.Resize(1, 2).Value = Array(xWs.Name, VBA.FileLen(xOutFile)) 'Kill xOutFile xIndex = xIndex + 1 End If Next '--- paste break ---
This comment was minimized by the moderator on the site
Here is a copy of the routine with a few enhancements I added. I had to break it into multiple posts due to the site limits. Public Sub WorksheetSizes() 'Update 20140526 ' https://www.extendoffice.com/documents/excel/1682-excel-check-size-of-each-sheet.html<br />' ' BS 4/4/2016: Modified to have a status bar and format the output. ' Fixed for hidden sheets that caused it to crash. ' Added support for Chart sheets Dim xWs As Object ' Worksheet or Chart Dim rng As Range Dim xOutWs As Worksheet Dim xOutFile As String Dim xOutName As String Dim xIndex As Long On Error GoTo ErrorHandler Application.Cursor = xlWait xOutName = "KutoolsforExcel" xOutFile = ThisWorkbook.Path & "\TempWb.xls" On Error Resume Next Application.DisplayAlerts = False Err = 0 Set xOutWs = Application.Worksheets(xOutName) If Err = 0 Then xOutWs.Delete Err = 0 End If With Application.ActiveWorkbook.Worksheets.Add(Before:=Application.Worksheets(1)) .Name = xOutName .Range("A1").Resize(1, 2).Value = Array("Worksheet Name", "Size") End With Set xOutWs = Application.Worksheets(xOutName) Application.ScreenUpdating = False xIndex = 1 Debug.Print ThisWorkbook.Path For Each xWs In Application.ActiveWorkbook.Worksheets If xWs.Name xOutName Then Application.StatusBar = "Calculating Worksheet Sizes, Sheet " & xIndex & " of " & ActiveWorkbook.Worksheets.count - 1 & " - " & xWs.Name Debug.Print "Calculating Worksheet Sizes, Sheet " & xIndex & " of " & ActiveWorkbook.Worksheets.count - 1 & " - " & xWs.Name DoEvents ' include this so CTRL+Break can be detected. '--- paste break ---
This comment was minimized by the moderator on the site
Hey Ben, Could you repaste the whole string of text with items #2 and #4 from your email added in? MY VBA knowledge is pretty limited and I'm not sure exactly where to add them into the For loop. My workbook has a number of hidden sheets and keeps crashing during the macro execution. Thanks, Bob
This comment was minimized by the moderator on the site
Thanks for providing the code snippet to the public. It's one of the better routines I found. Here are a few tweaks to it: 1) Add "Dim xIndex as Long" to the top if you're using Option Explicit. 2) Add this inside the For loop to handle hidden sheets (otherwise it crashes): xWs.Visible = xlSheetVisible 3) If you have full page "chart" sheets, you need to copy the code for the loop and iterate through the Application.ActiveWorkbook.Charts collection. If you do this, change the declaration of xWs from "Sheet" to "Object". 4) For a cheap status indicator (or for debugging issues) add this line inside the For loop: Debug.Print "Calculating Worksheet Sizes, Sheet " & xIndex & " of " & ActiveWorkbook.Worksheets.count - 1 & " - " & xWs.Name
This comment was minimized by the moderator on the site
Very helpful. Thank you!!
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations