简体   繁体   中英

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. 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.

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.

Name Manager References:

  • Cell Reference IsRequestDetailsFilled = Cell O2. This cell has formula where it's logic will simply state "Yes" or "No".
  • Cell Reference BusinessNeed = Merged Cells B13:F16

The objective is for the macro to refer to IsRequestDetailsFilled. If it is "No", then lock BusinessNeed. If it is "Yes", then unlock 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. 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. 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.

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. By default, Excel will create named ranges with a workbook-wide scope. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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