gpt4 book ai didi

vba - 尽管 UserInterFaceOnly=True 并且没有合并单元格,但无法设置锁定属性

转载 作者:行者123 更新时间:2023-12-04 20:35:24 24 4
gpt4 key购买 nike

我正在设置 Locked小范围的属性,但代码失败并出现熟悉的 1004 cannot set the Locked property of the Range class , 类似于 this problem .

  • 有问题的范围不包含任何合并的单元格
  • 我的工作表被 UserInterFaceOnly=True 锁定

  • 我没有想法 - 我怎样才能找出导致失败的原因?

    编辑:我没有提到我正在锁定工作表,保存它,关闭并重新打开 - 以及设置 Locked 的代码-属性在 Workbook_Open 中触发.

    这会导致重新打开工作簿 removes the interface-only protection, leaving the entire sheet protected 导致的设计问题。 .感谢@CLR 让我走上了这条路,如果他决定将其作为答案提交,则归功于他。

    开锁密码:
    Sub LockSheet()
    If ws1.ProtectContents = False Then ws1.Protect Password:="1", UserInterFaceOnly:=True
    End Sub

    工作表 protected 时失败的代码片段(但如果我取消保护工作表则有效):
    With summaryRange
    .Locked = Not (someBoolVar) ' <-- 1004 Cannot set Locked etc.
    .FormulaHidden = Not (someBoolVar)
    End With

    其中 summaryRange 是合格的并且可以在代码的其他部分工作:
    Set summaryRange = ws1.Range(firstSummaryColumn & "4:" & lastSummaryColumn & lastRow)

    &还验证了编译器的工作:
    ? Module1.summaryRange.Address
    $J$4:$M$50

    最佳答案

    我试图重现您的问题,但不能,使用下面的 subs 来模仿您的代码。

    片材保护:

    Sub LockSheet()
    Dim ws1 As Worksheet
    Set ws1 = ThisWorkbook.Sheets("Sheet1")

    If ws1.ProtectContents = False Then
    ws1.Protect Password:="1", UserInterFaceOnly:=True
    Else
    ws1.Unprotect Password:="1"
    End If
    End Sub

    范围锁定:
    Sub lockit()
    Dim ws1 As Worksheet
    Set ws1 = ThisWorkbook.Sheets("Sheet1")
    Dim someBoolVar As Boolean
    someBoolVar = True
    Dim summaryRange As Range
    Set summaryRange = ws1.Range("$J$4:$M$50")

    With summaryRange
    .Locked = Not someBoolVar ' <-- No error triggered here
    .FormulaHidden = Not someBoolVar
    End With
    End Sub

    可能的原因:
  • 您的 summaryRange未正确定义:您在 Q 中揭穿了这一点。
  • 床单锁定发生了一些可疑的事情:我已经在上面揭穿了这一点。
  • 您的 someBoolVar没有正确定义。注意在我上面的代码中,我定义了 someBoolVarTrue ,并且代码有效。尝试调试?someBoolVar在即时窗口中查看它在使用之前是什么。编辑:你也揭穿了这一点。


  • 编辑:

    正如评论中所建议的,您的问题可能是 UserInterfaceOnly标志被重置为 False当工作簿关闭时。为了避免这一点,您必须在工作簿打开时重新应用保护。这个子将实现:
    Sub reprotect()
    ' Called from the Workbook_Open event
    ' Cycle through sheets
    Dim sh As Worksheet
    For Each sh In ThisWorkbook.Sheets
    ' If protected, reprotect to reset UserInterfaceOnly flag
    If sh.ProtectContents = True Then
    sh.Unprotect Password:="1"
    sh.Protect Password:="1", UserInterfaceOnly:=True
    End If
    Next sh
    End Sub

    幸运的是, Locked范围的属性不受工作簿关闭的影响,因此您不必再次重新应用该条件!

    Office VBA 文档没有解决这个问题,但 VB documentation (通常可比较)事实上:

    If you apply this method with the UserInterfaceOnly argument set to true and then save the workbook, the entire worksheet (not just the interface) will be fully protected when you reopen the workbook. To re-enable the user interface protection after the workbook is opened, you must again apply this method with UserInterfaceOnly set to true.

    关于vba - 尽管 UserInterFaceOnly=True 并且没有合并单元格,但无法设置锁定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43253075/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com