如何lockingexcel vba中的行之间

我想根据两列的值locking在Excel表格的行之间,我有以下代码,但它使整个表保护。
代码是:

还有一个问题,当循环转到其他部分它抛出“无法设置范围类的locking属性”的代码是:

Do While xlsht.Cells(i, 1) <> vbNullString If (CStr(xlsht.Cells(i, 54).Value) <> "" And (CStr(Format(xlsht.Cells(i, 55).Value, "dd-MMM-yyyy")) = CStr(Format(Now, "dd-MMM-yyyy")))) Then .Cells.Locked = False .Range("A" & i & " : " & "BH" & i).Cells.Locked = True .Range("A" & i & " : " & "BH" & i).Interior.Color = RGB(255, 255, 0) .Protect Password:=admin Else .Cells.Locked = False .Range("A" & i & " : " & "AC" & i).Cells.Locked = True .Range("AE" & i & " : " & "AT" & i).Cells.Locked = True .Range("BB" & i & " : " & "BH" & i).Cells.Locked = True .Protect Password:=admin End If i = i + 1 Loop End With 

你可能会在这样的事情之后:

  Dim i As Long i = 1 With Worksheets("mySheetName") '<--| change "mySheetName" to your actual sheet name Do While .Cells(i, 1) <> "" If (.Cells(i, 54).Value = "abc" And .Cells(i, 55).Value = "def") Then Intersect(.Range("A:BH"), .Rows(i)).Locked = True i = i + 1 Loop .Protect Password:="admin" End With 

默认情况下,整个工作表是Locked(一个Range或者Cell的属性)。

而且你只能保护整个表格。

所以你必须首先解开表格的其余部分!

 i = 1 With xlsht .Unprotect Password:=admin .Cells.Locked = False Do While xlsht(i, 1) <> vbNullString If .Cells(i, 54).Values = "abc" And .Cells(i, 55).Values = "def" Then 'here is checking the column depends the row is get lock or not .Range("A" & i & ":BH" & i).Cells.Locked = True i = i + 1 End If Loop .Protect Password:=admin End With 'xlsht 

第二个问题

 i = 1 With xlsht .Unprotect Password:=admin .Cells.Locked = False Do While .Cells(i, 1).Value <> vbNullString If CStr(.Cells(i, 54).Value) <> vbNullString And CDate(.Cells(i, 55).Value) = Date Then With .Range("A" & i & ":BH" & i) .Cells.Locked = True .Interior.Color = RGB(255, 255, 0) End With '.Range("A" & i & ":BH" & i) Else .Range("A" & i & ":AC" & i).Cells.Locked = True .Range("AE" & i & ":AT" & i).Cells.Locked = True .Range("BB" & i & ":BH" & i).Cells.Locked = True End If i = i + 1 Loop .Protect Password:=admin End With 'xlsht