Excel VBA:使用循环时文件非常大

时间:2018-02-20 13:05:38

标签: excel vba excel-vba

我目前有一张excel电子表格,只有60多张,我使用了一些Vba代码,如下所示,保护每张纸不同于某些列,这对文件大小没有影响

Private Sub ProtectWorksheets_Click()
'Protects the totals and summary sheets and sets password

Worksheets("Project Totals").Unprotect Password:="BIM"
Worksheets("Project Totals").Range("A1:Z10000").Locked = True
Worksheets("Project Totals").Protect Password:="BIM"

Worksheets("Initial Discipline Totals").Unprotect Password:="BIM"
Worksheets("Initial Discipline Totals").Range("A1:Z10000").Locked = True
Worksheets("Initial Discipline Totals").Protect Password:="BIM"

Worksheets("Discipline Totals").Unprotect Password:="BIM"
Worksheets("Discipline Totals").Range("A1:Z10000").Locked = True
Worksheets("Discipline Totals").Protect Password:="BIM"

Worksheets("Initial Summary Chart").Unprotect Password:="BIM"
Worksheets("Initial Summary Chart").Range("A1:Z10000").Locked = True
Worksheets("Initial Summary Chart").Protect Password:="BIM"

Worksheets("Summary Chart").Unprotect Password:="BIM"
Worksheets("Summary Chart").Range("A1:Z10000").Locked = True
Worksheets("Summary Chart").Protect Password:="BIM"

Worksheets("Summary Table").Unprotect Password:="BIM"
Worksheets("Summary Table").Range("A1:Z10000").Locked = True
Worksheets("Summary Table").Protect Password:="BIM"


'Protects remaining worksheets while unlocking the comments and discpiline fields

Worksheets("1").Unprotect Password:="BIM"
Worksheets("1").Range("D5:D10000").Locked = False
Worksheets("1").Range("I5:I10000").Locked = False
Worksheets("1").Protect Password:="BIM"

Worksheets("2").Unprotect Password:="BIM"
Worksheets("2").Range("D5:D10000").Locked = False
Worksheets("2").Range("I5:I10000").Locked = False
Worksheets("2").Protect Password:="BIM"


**This is repeated for the next 60 sheeets


End Sub

当运行上述内容时,我的文件大小完全可以达到8mb。

但是,如果上面的代码被替换为以下代码,我最终会得到一个超过45mb的文件。

Private Sub ProtectWorksheets_Click()


Dim ws As Worksheet




'Protects remaining worksheets while unlocking the comments and discpiline fields


For Each ws In Sheets
ws.Unprotect Password:="BIM"
ws.Range("A1:Z10000").Locked = True
ws.Protect Password:="BIM"
Next



For Each ws In Sheets
If ws.Name <> "Front Page" And ws.Name <> "Admin" And ws.Name <> "Project 
Totals" And ws.Name <> "Initial Discipline Totals" And ws.Name <> 
"Discipline Totals" And ws.Name <> "Initial Summary Chart" And ws.Name <> 
"Summary Chart" And ws.Name <> "Summary Chart Table" Then
ws.Unprotect Password:="BIM"
ws.Range("D5:D10000").Locked = False
ws.Range("I5:I10000").Locked = False
ws.Protect Password:="BIM"
End If
Next


MsgBox ("All Sheets protected successfully")


End Sub

在使用看起来比第一个更好的代码时,为什么我最终会得到如此大的文件大小,我们将非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我重新创建(并简化了)下面的代码,并尝试了两件事:

  1. 仅使用UsedRange
  2. 将锁定的单元格限制为Intersect中的单元格
  3. 保存为.xlsb(2MB)而不是.xlsm(18MB)
  4. Private Sub ProtectWorksheets_Click()
        Dim ws As Worksheet
    
        For Each ws In Sheets
            ws.Unprotect Password:="BIM"
            Intersect(ws.Range("A:Z"), ws.UsedRange).Locked = True
            Select Case ws.Name
                Case "Front Page", "Admin", "Project Totals", "Initial Discipline Totals", "Discipline Totals", "Initial Summary Chart", "Summary Chart", "Summary Chart Table"
                    ws.Protect Password:="BIM"
                Case Else
                    Intersect(ws.Range("D5:D10000"), ws.UsedRange).Locked = False
                    Intersect(ws.Range("I5:I10000"), ws.UsedRange).Locked = False
                    ws.Protect Password:="BIM"
            End Select
        Next ws
    
        MsgBox ("All Sheets protected successfully")
    End Sub
    

    我还使用以下潜艇进行测试:

    Public Sub MakeSheets()
        Dim i As Long
        For i = 1 To 59
            ThisWorkbook.Worksheets.Add
        Next i
    End Sub
    
    Public Sub InsertData()
        Dim ws As Worksheet
        For Each ws In ThisWorkbook.Worksheets
            ws.Range("A1:I10000").Value = 1
        Next ws
    End Sub