【问题标题】:Lock range of Cells in row based on adjacent cell value根据相邻单元格值锁定行中的单元格范围
【发布时间】:2017-12-21 05:19:21
【问题描述】:

我正在使用 vba 代码使用两组工作表来审查和批准某些数据行: 第一个是“View_Form”,我们在特定表单视图中查看输入的数据。 第二个是“Tracker”,所有数据都从外部下载存储。

在“View_Form”表中,我们选择文件 ID 并显示与其相关的所有数据,如果一切正常,我们单击宏按钮“已批准”,文本“已批准”进入与所选文件 ID 相邻的 HR 列否则它将是空白的。

它可以工作,但我们仍然可以编辑我想要限制的“已批准”行。也就是说,如果 HR 单元格包含文本“已批准”,则 A:HR 中的特定行应该被锁定或应该限制用户编辑。

应该允许用户在使用密码取消保护工作表后进行编辑,例如密码为 123。

谁能帮我解决这个问题...

当前审批代码:

Sub Approval()
Dim found As Range 'define variables
Dim SelectedFileID As String

'Approval function
SelectedFileID = Sheets("View_Form").Range("SelFileID").Value 'get the currently selected File ID

Set found = Sheets("Tracker").Range("B:B").Find(What:=SelectedFileID) 'find the file ID in the Sheet Tracker
    If Not found Is Nothing Then 'if found
        Sheets("Tracker").Cells(found.Row, 226).Value = "Approved" 'change the value of the row it was found, but column 226 which is column HR
    Else
        MsgBox "ID not found in Sheet Tracker!", vbInformation 'if not found then show message
    End If
    ActiveWorkbook.Save '---------------Save workbook
    Application.DisplayAlerts = False
End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    这将锁定第 226 列包含“已批准”的所有行(您仍然可以使用密码解锁):

    Sub Picture1_Click()
    Dim found As Range 'define variables
    Dim SelectedFileID As String
    
    SelectedFileID = Sheets("View_Form").Range("SelFileID").Value 'get the currently selected File ID
    Application.DisplayAlerts = False    
    Set found = Sheets("Tracker").Range("B:B").Find(What:=SelectedFileID) 'find the file ID in the Sheet Tracker
        If Not found Is Nothing Then 'if found
            Sheets("Tracker").Unprotect Password:="1234" 'change the password to whatever you wish, this unlocks the sheet
            Sheets("Tracker").Cells(found.Row, 226).Value = "Approved" 'change the value of the row it was found, but column 226 which is column HR
            Sheets("Tracker").Range("A1:HR500").Cells.Locked = False 'keeps range unlocked
            LastRow = Sheets("Tracker").Cells(Sheets("Tracker").Rows.Count, "A").End(xlUp).Row
            For i = 3 To LastRow
                If Sheets("Tracker").Cells(i, 226).Value = "Approved" Then
                    Sheets("Tracker").Rows(i).Cells.Locked = True
                End If
            Next i
            Sheets("Tracker").Protect Password:="1234" 'protect the sheet after updating to Approved on Column HR
        Else
            MsgBox "ID not found in Sheet Tracker!", vbInformation 'if not found then show message
        End If
    ActiveWorkbook.Save '---------------Save workbook
    Application.DisplayAlerts = True
    End Sub
    

    【讨论】:

    • 收到两条错误消息作为变量 lastrow 和未找到“i”。但是,我使用 Dim Lastrow As Long 和 Dim i As Long 修改了代码,并通过锁定整行来工作。感谢您的帮助。
    • 我观察到上面的代码批准代码只运行一次它说在工作表跟踪器中找不到 ID 可用。我在上面的代码中也对单元格计数做了一些更改。
    • @Sanoj,这按预期对我有用,您所做的第一条评论是因为您使用的是 Option Explicit 而我没有,所以 VBA 告诉您声明变量,但我没有不了解您最近的评论,因为 ID 是在 View_Form 的下拉列表中选择的,所以我想这与您如何填充该列表框有关...
    • 再次粘贴代码并正常工作,感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    相关资源
    最近更新 更多