简体   繁体   English

如何根据另一个单元格的值锁定单元格并执行数据验证

[英]How to Lock Cell based on the value of the another cell and perform data validation

I am very new to VBA code. 我对VBA代码非常陌生。 Now I was assigned to make a task that validate the data in an excel sheets. 现在,我被分配去执行一项任务,以验证Excel工作表中的数据。

For Example in my Column A , I have a drop down menu that enable the user to make a selection between "Yes" or "No" only. 例如,在我的A列中 ,有一个下拉菜单,使用户可以仅在“是”或“否”之间进行选择。

  • If user selected "Yes", cells in Column B and Column C will be marked as Mandatory and cannot be blank. 如果用户选择“是”,则B列和C列中的单元格将标记为必填,不能为空。 I want to put a validation error on this. 我想对此进行验证错误。

     **Example 1: If A1 and A30 == YES** 
    • B1 and C1, B30 and C30 are mandatory B1和C1,B30和C30是必填项

    • Will fill a color to the mandatory cells and remove the fill color when the cells have value already 将为必填单元格填充颜色,并在单元格已具有值时删除填充色

    • Throw a validation error when these cells are blank and if exceeds the number of characters required. 当这些单元格为空白并且超过所需的字符数时,将引发验证错误。

      Example 2: If A99 == NO 示例2:如果A99 ==否

    • B99 will be locked and the user will not be allowed to enter a data on this cell. B99将被锁定,并且不允许用户在此单元格上输入数据。 Possible that we can add a cell value as "NA" to avoid confusion 可能我们可以将单元格值添加为“ NA”以避免混淆

I was able to capture this using the data validation and conditional formatting. 我能够使用数据验证和条件格式捕获它。 However, I cannot perform the locked functionality since as per research, I need a VBA code for this one. 但是,我无法执行锁定功能,因为根据研究,我需要为此使用VBA代码。

Something like this should work. 这样的事情应该起作用。

Put it into the code module for the sheet you want to apply it to. 将其放入要应用于其的图纸的代码模块中。

Private Sub worksheet_change(ByVal target As Range)

    ''''' CHECK IF THE CHANGED CELL IS IN RANGE A1:A99 (OR ANY OTHER RANGE YOU DEFINE)
    If Not Intersect(target, Range("A1:A99")) Is Nothing Then

        ''''' UNPROTECT THE SHEET
        ActiveSheet.Unprotect

        ''''' IF THE CELL CHANGED IS NOW 'YES'
        If target = "Yes" Then

            ''''' WE DEFINE HOW MANY COLUMNS TO MOVE ACROSS FROM THE CELL THAT CHANGED AND DO THE ACTIONS IN THE CODE BELOW
            ''''' SO IN THIS EXAMPLE WE'RE MOVING ACROSS 1 CELL TO B1 AND THEN 2 CELLS TO C1
            ''''' SO TO GET TO AA1 AND AB2 WE'D DO i = 26 to 27
            ''''' IF WE WANTED TO ACCESS AA1 ALL THE WAY THROUGH TO AZ1 WE'D DO i = 26 to 51
            For i = 1 To 2

                ''''' MOVE ACROSS i NUMBER OF CELLS FROM THE CELL THAT CHANGED
                With target.Offset(0, i)

                    ''''' UNLOCK THE CELL
                    .Locked = False

                    '''''SET THE CONDITIONAL FORMATTING
                    .FormatConditions.Add Type:=xlExpression, Formula1:="=ISBLANK(" & target.Offset(0, i).Address & ")"
                    With .FormatConditions(.FormatConditions.Count)
                        .SetFirstPriority
                        .Interior.ColorIndex = 37
                    End With

                End With

            ''''' INCREASE i BY 1 AND LOOP TO AFFECT THE NEXT CELL
            Next i

        ''''' IF THE CELL CHANGED IS NOW 'NO'
        ElseIf target = "No" Then

            ''''' WE DEFINE HOW MANY COLUMNS TO MOVE ACROSS FROM THE CELL THAT CHANGED AND DO THE ACTIONS IN THE CODE BELOW
            For i = 1 To 2

                ''''' MOVE ACROSS i NUMBER OF CELLS FROM THE CELL THAT CHANGED
                With target.Offset(0, i)

                    ''''' SET THE CELL VALUE TO BLANK
                    .Value = ""

                    ''''' LOCK THE CELL
                    .Locked = True

                    ''''' REMOVE THE CONDITIONAL FORMATTING
                    .FormatConditions.Delete

                        ''''' ADD NEW CONDITIONAL FORMATTING HERE IF REQUIRED

                End With

            ''''' INCREASE i BY 1 AND LOOP TO AFFECT THE NEXT CELL
            Next i

        End If

        '''''PROTECT THE SHEET
        ActiveSheet.Protect

    End If

End Sub

Be sure to set locked to false in your A column where the drop down lists are or users won't be able to change the drop down value while the sheet is locked. 确保在下拉列表所在的A列中将locked设置为false,否则工作表被锁定时用户将无法更改下拉值。

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

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