Comment verrouiller entre les lignes dans excel vba

Je veux verrouiller entre les lignes de la feuille d'excel de la ligne en fonction de la valeur des deux colonnes, j'ai le code suivant avec moi, mais cela rend la feuille entière protégée.
le code est:

il y a un autre problème lorsque la boucle va en autre partie, il "incapable de définir la propriété verrouillée de la class de plage" le code est:

Do While xlsht.Cells(i, 1) <> vbNullSsortingng 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 

vous pouvez être après quelque chose comme ceci:

  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 

Par défaut, la feuille entière est verrouillée (propriété d'une plage ou d'une cellule).

Et vous pouvez seulement protéger une feuille ENTIER.

Vous devrez alors retirer le rest de la feuille!

 i = 1 With xlsht .Unprotect Password:=admin .Cells.Locked = False Do While xlsht(i, 1) <> vbNullSsortingng 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 

Deuxième question

 i = 1 With xlsht .Unprotect Password:=admin .Cells.Locked = False Do While .Cells(i, 1).Value <> vbNullSsortingng If CStr(.Cells(i, 54).Value) <> vbNullSsortingng 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