(VBA)当我使用锁定属性锁定大范围时,为什么我的工作表变得非常大?

时间:2017-05-04 14:03:40

标签: excel vba excel-vba

我有一个大约11MB的工作表(85k行,70列)。然后我运行以下宏来解锁两个范围,安全我的工作表,然后工作表是87MB大。为什么会这样?我怎么能改变它?

PS:范围非常大

Sub unprotect()
    Dim lastRow, lastColumn, firstRow, firstColumn As Variant

    ActiveSheet.unprotect

    firstRow = 2
    firstColumn = 32
    lastRow = ActiveSheet.Cells(Rows.Count, 5).End(xlUp).row '85k
    lastColumn = 350

    With ActiveSheet.Range(Cells(firstRow, 1), Cells(lastRow, 2))
    .Locked = False 'unlock the cells, so they can be edited in a protected sheet
    End With

    With ActiveSheet.Range(Cells(firstRow, firstColumn), Cells(lastRow, lastColumn))
    .Locked = False
    End With

    ActiveSheet.protect 'protect the sheet so only unlocked cells can be edited
End Sub

2 个答案:

答案 0 :(得分:0)

试试这样:

Sub unprotect()

    Dim lastRow         as long
    dim lastColumn      as long
    dim firstRow        as long
    dim firstColumn     as long

    With ActiveSheet
        .unprotect

        firstRow = 2
        firstColumn = 32
        lastRow = .Cells(.Rows.Count, 5).End(xlUp).row '85k
        lastColumn = 350

        .Range(.Cells(firstRow, 1), .Cells(lastRow, 2)).Locked = False 
        .Range(.Cells(firstRow, firstColumn), .Cells(lastRow, lastColumn)).Locked = False
        .protect 
    End With

End Sub

以下是关于范围对象的一点 - https://msdn.microsoft.com/en-us/library/office/ff194567.aspx

如果不起作用,请使用以下代码删除其他样式(如果有):

Sub RemoveTheStyles()

    Dim style               As style
    Dim l_counter           As Long
    Dim l_total_number      As Long

    On Error Resume Next

    l_total_number = ActiveWorkbook.Styles.Count
    Application.ScreenUpdating = False

    For l_counter = l_total_number To 1 Step -1

        Set style = ActiveWorkbook.Styles(l_counter)

        If (l_counter Mod 500 = 0) Then
            DoEvents
            Application.StatusBar = "Deleting " & l_total_number - l_counter + 1 & " of " & l_total_number & " " & style.Name
        End If

        If Not style.BuiltIn Then style.Delete

    Next l_counter

    Application.ScreenUpdating = True
    Application.StatusBar = False
    Debug.Print "READY!"

    On Error GoTo 0
End Sub

这两种解决方案中的一种应该给出一些结果。

答案 1 :(得分:0)

主要原因是,通过将cells.locked设置为false,您已经从使用85K行70列(约600万个单元格)到使用85k行少于350列(不到3000万个单元格),所以大约5倍左右。