如何根据其他sheet2上的值禁用Sheet1中的rows

时间:2015-06-01 06:15:15

标签: excel-vba vba excel

我试图禁用Sheet1中的行,该范围基于基于Sheet2的值。工作表将包含带标题的行,而Sheet2将具有值为0或1的标题。根据Sheet2中的标题值是0还是1,它将在Sheet1中禁用或启用。

0表2中的值 - 应在Sheet1中禁用行。

Sheet2中的值 - 应在Sheet1中启用行。

1 个答案:

答案 0 :(得分:0)

您好请参阅下面的代码,代码将在Sheet2范围A1至A50中查看" 0"和" 1"然后相应地锁定sheet1中的相同行:

Sub LockingRows()
Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim c As Range


Set wb = ActiveWorkbook
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("Sheet2")

ws1.Unprotect Password:="pass" '<--- Your password
ws1.Cells.Locked = False
For Each c In ws2.Range("A1:A50").Cells '<--- Your range to look in sheet2 for 0 and 1
    If c.Value = 1 Then
        ws1.Rows(c.Row & ":" & c.Row).Locked = True
        ElseIf c.Value = 0 Then
        ws1.Rows(c.Row & ":" & c.Row).Locked = False
    End If
Next
ws1.Protect Password:="pass", UserInterfaceOnly:=True '<--- Your password

End Sub