繁体   English   中英

根据其他单元格中的条件锁定单元格

[英]Locking cells based on criteria in other cells

我有一个电子表格,该电子表格将用于管理各种项目所需的文档和信息。 我希望根据其他单元格中输入的信息对其中一些单元格进行着色和锁定。

在详细介绍之前,我应该提到我对宏和VBA完全陌生,因此我的知识非常基础。 这些列可能会更改,因此除了代码外,我想知道如何编辑代码以包括其他列或如何删除它们。 此外,我将需要将500多行数据应用于这些数据,最终产品将受到保护。

我在文档中添加了一个链接,该链接与我实际正在处理的文档相似(由于公司的安全性限制,我无法上载原始文档)。 他是我目前需要满足的条件:

  • 如果在B列中选择了“Single Source”EHPR列需要锁定。
  • 如果在C列中除了“Single Source”之外还选择了“≤ $50k”“≤ $100k” “Single Source” ,则F列将被锁定。
  • C列中的下拉列表可能会更改为手动输入的估算价格,因为当前范围可能会发生变化。
  • 如果“Bid”列中选择B ,如果“≤ $50k”“≤ $100k”栏选择C ,列PRAC需要锁定
  • 如果C列低于$2ML5A5批准都将被锁定
  • 如果C列低于$1ML4A4批准都将被锁定
  • 如果C列低于$500kL3A3批准都将被锁定
  • 如果C列低于$50kL2A2批准都将被锁定
  • L2A2批准将永远不会被锁定。

到目前为止,我在网上找到的所有代码均无法正常运行,因为我无法成功对其进行编辑以满足我的需求,因此将不胜感激。

https://www.dropbox.com/s/9memoq1hcab2a4e/Document%20Control_Test.xlsm?dl=0

作为参考,UserForm是VBA可以打开的表单,其工作方式类似于应用程序的精简版。 Excel曾经向您显示的任何错误提示都是UserForm(在幕后)的一个示例。 您可以从Visual Studio for Applications窗口在Excel中创建自己的窗口。

在开始做您想做的事情之前,您提到过希望C列是允许用户输入值的下拉列表。 Excel工作表不允许这样做。 它是一个下拉列表或用户输入的值,不能两者兼有。 鉴于选项列表在某些情况下可能不够用,您应该将其设置为用户输入的单元格(即没有下拉选项),然后验证在宏中输入的值。

您可以使用直接宏来执行所需的操作,并鉴于您对VBA代码应执行的操作的可变性的评论,我将成为一个异端,并说这可能是更易于维护的方法。

为此,您需要编辑工作表代码以添加事件处理程序,尤其是Worksheet_Change

从那里,您应该检查更改的单元格是应该导致宏运行的单元格。 根据问题的详细信息,您将需要在B和C列中查找单元格,如下所示

IF LEFT(Target.Address, 3) = "$B$" OR LEFT(Target.Address, 3) = "$C$" THEN

在那时,仅调用一个实际上执行繁重任务的不同函数并将其传递给行号可能是有意义的。 例:

FormatMgmt(RIGHT(Target.Address, LEN(TARGET.Address) - 3), ActiveSheet.Name)

然后给你一个例子,这个例子应该使您对如何列出的所有事情有所了解。

Sub FormatMgmt(rn as long, WSName as String)
    '
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets(ActiveSheet.Name)
    Dim CC As Range
    Set CC = ws.Range("C" & rn) 'I'm using a variable to refer to column C since we'll
        'need to use this cell quite a bit

    Application.ScreenUpdating 'Prevent Excel from updating what's displayed on your
        'computer monitor. This lets the macro run faster

    'Determine what's in Column B for the identified row
    Select Case LCase(ws.Range("B" & rn).Value) 'LCase forces all text to lower case, so
        'that capitalization doesn't mess up the comparison
        Case "single source"
            'Protect Columns E-H & P-R
            ws.Unprotect ("<whatever_your_password_is>") 'The password is optional, but
                'you need to unlock the sheet to change whether cells are protected

            ws.Range("E" & rn & ":H" & rn).Locked = True

            'Check if column F needs locked. Note, you said if <=50k OR <=100k selected
            'should do this. Since I'm assuming a user entered value, I have to take the
            'tigher constraint
            If CC.Value <= 50000 Then
                'Lock column F.
                ws.Range("F" & rn).Locked = True
            End If

            'Check if L3, L4, L5, A3, A4, or A5 need locked. Note: you're last bullet
            'says A2 & L2 should never be locked, which contradicts the bullet
            'immediately prior. I assumed the later bullet took precedence. Also, I
            'assumed the conditions in the last 5 bullets stacked (i.e. CC < $500k means
            'A3-A5 & L3-5 should be locked)
            If CC.Value < 2000000 Then
                ws.Range("L5").Locked = True
                ws.Range("A5").Locked = True
            End If

            If CC.Value < 1000000 Then
            '...
            'Fill in the rest of the code here. You should have enough to start
            '...
            'NOTE: Be sure to put in appropriate logic to UNDO any of the cell locking
            'if a cell's value changes as well

        'Relock the sheet
        WS.Protect"<whatever_your_password_is>")

        'At the very end of the macro be sure to turn screen updating back on (no
        'good doing all this if it doesn't show on the screen)
        Application.ScreenUpdating = True
End Sub

那应该足以让您入门。 如果不是,请在Google上搜索一个更具体的问题,您应该很快找到答案(这就是我学习VBA,VB.net,C#,SQL等的方式)

暂无
暂无

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

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