Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA在不锁定所有单元格的情况下保护工作表_Excel_Vba - Fatal编程技术网

Excel VBA在不锁定所有单元格的情况下保护工作表

Excel VBA在不锁定所有单元格的情况下保护工作表,excel,vba,Excel,Vba,我试图保护Excel电子表格中的标题。为此,我选择了整个工作表,转到“单元属性”,并取消选中“锁定”。然后,我只选择了第一行并选中了“锁定” 我的宏一次运行正常,然后再次运行时,我会收到与被锁定的工作表相关的错误,当我返回并检查工作表时,现在所有单元格都被再次锁定。我没有任何VBA代码指定锁定任何单元格。我运行此宏以保护图纸: Public Sub ProtectSheet(Optional sheetname As String) Dim thisSheet As Works

我试图保护Excel电子表格中的标题。为此,我选择了整个工作表,转到“单元属性”,并取消选中“锁定”。然后,我只选择了第一行并选中了“锁定”

我的宏一次运行正常,然后再次运行时,我会收到与被锁定的工作表相关的错误,当我返回并检查工作表时,现在所有单元格都被再次锁定。我没有任何VBA代码指定锁定任何单元格。我运行此宏以保护图纸:

Public Sub ProtectSheet(Optional sheetname As String)
    
    Dim thisSheet As Worksheet
    'This is to protect sheet from userinterface, but not from macros
    If IsMissing(sheetname) Then sheetname = ""
    If sheetname = "" Then
        Set thisSheet = ActiveWorkbook.ActiveSheet
    Else
        Set thisSheet = ActiveWorkbook.Worksheets(sheetname)
    End If
    
    thisSheet.Protect UserInterfaceOnly:=False, Contents:=True, DrawingObjects:=True, Scenarios:=True, AllowFormattingRows:=True, AllowFormattingColumns:=True, AllowFormattingCells:=True, AllowInsertingHyperlinks:=True, AllowSorting:=True, AllowFiltering:=True

End Sub
我创建了如下VBA代码,以取消对图纸的保护,选择“全部”并解锁,然后锁定第一行,然后保护。当我这样做的时候,它会起作用,但我不明白为什么我必须这样做

Public Sub ProtectSheet(Optional sheetname As String)
    
    Dim thisSheet As Worksheet
    'This is to protect sheet from userinterface, but not from macros
    If IsMissing(sheetname) Then sheetname = ""
    If sheetname = "" Then
        Set thisSheet = ActiveWorkbook.ActiveSheet
    Else
        Set thisSheet = ActiveWorkbook.Worksheets(sheetname)
    End If
    
    thisSheet.Unprotect
    thisSheet.Cells.Locked = False
    thisSheet.Rows(1).Locked = True
    thisSheet.Protect UserInterfaceOnly:=False, Contents:=True, DrawingObjects:=True, Scenarios:=True, AllowFormattingRows:=True, AllowFormattingColumns:=True, AllowFormattingCells:=True, AllowInsertingHyperlinks:=True, AllowSorting:=True, AllowFiltering:=True

End Sub

我想知道为什么我所有的手机都被锁定了,我不想在我认为不应该的时候添加额外的代码。在Excel中是否有一个错误导致锁定属性被设置,或者我丢失了这个代码中自动锁定的东西?

< p>如果问题是使用<代码>清除< /代码>,那么考虑创建一个单独的子来管理它,并调用它而不是<代码>清除< /代码> .<
Sub ClearButUnlocked(rng As Range)
    with rng
        .clear
        .cells.locked=false
    end with
end sub

…假设代码> RNG 只有一个锁状态,并不是锁定/解锁单元格的组合

< P>如果问题是使用<代码>清除< /代码>,那么考虑创建一个单独的子来管理它,调用它而不是<代码>清除< /代码> .<
Sub ClearButUnlocked(rng As Range)
    with rng
        .clear
        .cells.locked=false
    end with
end sub

…假设
rng
只有一个锁定状态,并且不是锁定/解锁单元格的混合

无法复制您报告的内容。很可能这里遗漏了什么我不确定可能是什么。我搜索了整个项目,没有.lock anywhere的实例(在更新代码以解决问题之前)-是否有任何其他VBA属性会间接影响.lock属性?是否使用
清除
@TimWilliams我从问题中假设Clear设置了“锁定”位。是的,我使用的是“Clear”,因此可以解释这个问题。有没有其他方法可以清除?我需要清除单元格中的所有内容,但我需要保持解锁状态。无法复制您正在报告的内容。很可能这里遗漏了什么我不确定可能是什么。我搜索了整个项目,没有.lock anywhere的实例(在更新代码以解决问题之前)-是否有任何其他VBA属性会间接影响.lock属性?是否使用
清除
@TimWilliams我从问题中假设Clear设置了“锁定”位。是的,我使用的是“Clear”,因此可以解释这个问题。有没有其他方法可以清除?我需要清除单元格中的所有内容,但我需要保持解锁状态。我的最终解决方案有点不同,但多亏了你的评论/回答,我才明白了这一点,这当然也会起作用。我从未意识到。清除还清除了锁定/解锁状态。链接文章也指出了这一点,但我确实需要清除格式设置(锁定除外),这样这个解决方案才能更好地工作。谢谢。我的最终解决方案有点不同,但多亏了你的评论/回答,我才明白了这一点,这当然也会奏效。我从未意识到。清除还清除了锁定/解锁状态。链接文章也指出了这一点,但我确实需要清除格式设置(锁定除外),这样这个解决方案才能更好地工作。非常感谢。