Hoppa till huvudinnehåll

Hur kontrollerar jag storleken på varje arbetsblad i arbetsboken?

Om du antar att du har en stor arbetsbok som innehåller flera kalkylblad, och nu vill du ta reda på storleken på varje kalkylblad för att bestämma vilket ark som ska minskas. Finns det några snabba metoder för att hantera denna uppgift?

Kontrollera storleken på varje kalkylblad med VBA-kod

Kontrollera storleken på varje kalkylblad med Kutools för Excel


pil blå höger bubbla Kontrollera storleken på varje kalkylblad med VBA-kod

Med följande VBA-kod kan du snabbt få storleken på varje kalkylblad i din arbetsbok. Gör så här:

1. Håll ner ALT + F11 knapparna och det öppnar Microsoft Visual Basic for Applications-fönstret.

2. Klicka Insert > Modulernaoch klistra in följande kod i Modulfönster.

VBA-kod: Kontrollera storleken på varje kalkylblad i en arbetsbok </ p>

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. Tryck sedan på F5 nyckel för att köra den här koden och ett nytt kalkylblad med namnet KutoolsforExcel kommer att införas i den aktuella arbetsboken som innehåller varje kalkylbladsnamn och filstorlek, och enheten är Bit. Se skärmdump:

doc-check-sheet-storlek 1


pil blå höger bubbla Kontrollera storleken på varje kalkylblad med Kutools för Excel

Om du har Kutools för Excel, med dess Dela arbetsbok verktyget kan du dela upp hela arbetsboken i separata filer och sedan gå till den specifika mappen för att kontrollera storleken på varje fil.

Kutools för Excel innehåller mer än 300 praktiska Excel-verktyg. Gratis att prova utan begränsning på 30 dagar. Hämta nu.

När du har installerat Kutools för Excel gör du med följande steg:

1. Öppna arbetsboken du vill kontrollera storleken på varje kalkylblad och klicka Företag > Arbetsbokens verktyg > Dela arbetsbok, se skärmdump:

doc-check-sheet-storlek 1

2. I Dela arbetsbok dialogrutan, kolla alla kalkylblad och klicka Split och ange sedan en mapp för att lägga till de nya arbetsboksfilerna. Se skärmdumpar:

doc-check-sheet-storlek 3
-1
doc-check-sheet-storlek 4

3. Och sedan sparas varje kalkylblad i din nuvarande arbetsbok som en separat Excel-fil, du kan gå till din specifika mapp för att kontrollera storleken på varje arbetsbok.

doc-check-sheet-storlek 1

Om du vill veta mer om den här funktionen för delad arbetsbok


Relaterade artiklar:

Hur delar jag en arbetsbok för att separera Excel-filer i Excel?

Hur exporterar och sparar jag ark och kalkylblad som en ny arbetsbok i Excel?

Bästa kontorsproduktivitetsverktyg

🤖 Kutools AI Aide: Revolutionera dataanalys baserat på: Intelligent utförande   |  Generera kod  |  Skapa anpassade formler  |  Analysera data och generera diagram  |  Anropa Kutools funktioner.
Populära funktioner: Hitta, markera eller identifiera dubbletter   |  Ta bort tomma rader   |  Kombinera kolumner eller celler utan att förlora data   |   Rund utan formel ...
Superuppslag: Flera kriterier VLookup    VLookup med flera värden  |   VSök över flera ark   |   Fuzzy Lookup ....
Avancerad rullgardinslista: Skapa snabbt en rullgardinslista   |  Beroende rullgardinslista   |  Flervals-rullgardinslista ....
Kolumnhanterare: Lägg till ett specifikt antal kolumner  |  Flytta kolumner  |  Växla synlighetsstatus för dolda kolumner  |  Jämför intervall och kolumner ...
Utvalda funktioner: Rutnätsfokus   |  Designvy   |   Stor formelbar    Arbetsbok & Bladhanterare   |  Resursbibliotek (Automatisk text)   |  Datumväljare   |  Kombinera arbetsblad   |  Kryptera/Dekryptera celler    Skicka e-postmeddelanden efter lista   |  Superfilter   |   Specialfilter (filtrera fet/kursiv/genomstruken...) ...
Topp 15 verktygssatser12 text verktyg (lägga till text, Ta bort tecken, ...)   |   50+ Diagram Typer (Gantt Chart, ...)   |   40+ Praktiskt Formler (Beräkna ålder baserat på födelsedag, ...)   |   19 Införande verktyg (Infoga QR-kod, Infoga bild från sökväg, ...)   |   12 Konvertering verktyg (Siffror till ord, Valutaväxling, ...)   |   7 Slå ihop och dela verktyg (Avancerade kombinera rader, Dela celler, ...)   |   ... och mer

Uppgradera dina Excel-färdigheter med Kutools för Excel och upplev effektivitet som aldrig förr. Kutools för Excel erbjuder över 300 avancerade funktioner för att öka produktiviteten och spara tid.  Klicka här för att få den funktion du behöver mest...

Beskrivning


Fliken Office ger ett flikgränssnitt till Office och gör ditt arbete mycket enklare

  • Aktivera flikredigering och läsning i Word, Excel, PowerPoint, Publisher, Access, Visio och Project.
  • Öppna och skapa flera dokument i nya flikar i samma fönster, snarare än i nya fönster.
  • Ökar din produktivitet med 50 % och minskar hundratals musklick för dig varje dag!
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