简体   繁体   English

基于条件逻辑的锁定/解锁单元格引用

[英]Lock/Unlock Cell References Based on Conditional Logic

I am creating a report request form in Excel that essentially forces the requester to go step by step within the document to ensure we have everything filled out.我正在 Excel 中创建一个报告请求表,它基本上强制请求者在文档中逐步进行,以确保我们填写了所有内容。 As a control, I'm looking to lock the inputs in the further steps until the current step has been completed (ie simply putting text in a cell).作为控件,我希望在后续步骤中锁定输入,直到当前步骤完成(即,只需将文本放入单元格中)。 I've put names in the Name Manager in Excel for each of the input steps.我已经在 Excel 的名称管理器中为每个输入步骤输入了名称。

For the life of me I can't get my code to work.对于生活,我无法让我的代码工作。 I've even just tried to implement a MsgBox to make sure the IF is working correctly, but nothing appears.我什至试图实现一个 MsgBox 以确保 IF 正常工作,但什么也没出现。

Name Manager References:名称管理器参考:

  • Cell Reference IsRequestDetailsFilled = Cell O2.单元格引用 IsRequestDetailsFilled = 单元格 O2。 This cell has formula where it's logic will simply state "Yes" or "No".该单元格具有公式,其逻辑将简单地说明“是”或“否”。
  • Cell Reference BusinessNeed = Merged Cells B13:F16单元格参考 BusinessNeed = 合并单元格 B13:F16

The objective is for the macro to refer to IsRequestDetailsFilled.目标是让宏引用 IsRequestDetailsFilled。 If it is "No", then lock BusinessNeed.如果为“否”,则锁定 BusinessNeed。 If it is "Yes", then unlock BusinessNeed.如果是“是”,则解锁 BusinessNeed。

My code:我的代码:

Private Sub Is_RequiredRequesterDetails_Filled()

    'Dim CurrentWorksheet As Worksheet
    Dim IsRequestDetailsFilled As Range
    Dim BusinessNeed As Range

    'Set CurrentWorksheet = Worksheets("New Report Request")
    Set IsRequestDetailsFilled = Range("IsRequestDetailsFilled").Value
    Set BusinessNeed = Range("BusinessNeed").Value
    
    If IsRequestDetailsFilled = "No" Then
        MsgBox Prompt:="Locked"
        'CurrentWorksheet.BusinessNeed.Locked = True
    Else
        MsgBox Prompt:="Unlocked"
        'CurrentWorksheet.BusinessNeed.Locked = False
    End If
End Sub

Maybe I'm going insane because I haven't stopped working since 6 AM and it's now 10:30 PM... but I can't seem to find this answer from Googling it.也许我快疯了,因为我从早上 6 点开始就没有停止工作,现在是晚上 10 点 30 分……但我似乎无法从谷歌搜索中找到这个答案。 I've tried several alternatives.我已经尝试了几种替代方法。 I want the code to be readable, so ideally I would like to use the Reference Names I've designated.我希望代码可读,所以理想情况下我想使用我指定的参考名称。

If there's a better way to achieve what I am saying, please feel free to suggest that too.如果有更好的方法来实现我所说的,也请随时提出建议。

Thanks!谢谢!

Consider the logic of this excerpt from your code.考虑这段代码摘录的逻辑。

Dim IsRequestDetailsFilled As Range
Set IsRequestDetailsFilled = Range("IsRequestDetailsFilled").Value

The value property of Range("IsRequestDetailsFilled") is a variant - maybe a number or a string - but IsRequestDetailsFilled is a range. Range("IsRequestDetailsFilled") 的 value 属性是一个变体 - 可能是数字或字符串 - 但IsRequestDetailsFilled是一个范围。 The appearance is that you are conflating the range itself with its value.外观是您将范围本身与其值混为一谈。 The value property is just one of the many properties every range has, like Row, Column, Font, Fill etc. It so happens that the Value property is the default and therefore can be omitted which makes it the source of countless miscomprehensions. value 属性只是每个范围具有的众多属性之一,如 Row、Column、Font、Fill 等。 碰巧 Value 属性是默认值,因此可以省略,这使得它成为无数误解的根源。

Below is the correct syntax for assigning a range object to a variable declared as range.下面是将范围对象分配给声明为范围的变量的正确语法。

Dim IsRequestDetailsFilled As Range
Set IsRequestDetailsFilled = Range("IsRequestDetailsFilled")

In practice you may not need that.在实践中,您可能不需要那个。 The following code will work just as well and is perhaps easier to read.下面的代码也能正常工作,而且可能更容易阅读。

Private Sub Is_RequiredRequesterDetails_Filled()
    
    If Range("IsRequestDetailsFilled").Value = "No" Then
        MsgBox Prompt:="Locked"
        'CurrentWorksheet.BusinessNeed.Locked = True
    Else
        MsgBox Prompt:="Unlocked"
        'CurrentWorksheet.BusinessNeed.Locked = False
    End If
End Sub

If Range("IsRequestDetailsFilled") is declared with a scope for the worksheet only the worksheet must be specified.如果使用工作表的范围声明 Range("IsRequestDetailsFilled"),则仅必须指定工作表。 By default, Excel will create named ranges with a workbook-wide scope.默认情况下,Excel 将创建具有工作簿范围范围的命名区域。 You can check and set the scope in the Name Manager.您可以在名称管理器中检查和设置范围。 If the name was created with a workbook-wide scope VBA will find it anywhere in the workbook and you don't need to mention the tab in the code.如果名称是使用工作簿范围的范围创建的,则 VBA 将在工作簿中的任何位置找到它,您无需在代码中提及该选项卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM