XL 2019 Verrouiller que si cellules vides

Tioneb_h

XLDnaute Nouveau
Bonjour,

Je voudrais verrouiller les cellules que si elles sont vides...
La macro verrouille bien les cellules vides mais déverrouille également les cellules non vides et verrouillées...
voir pièce jointe.
quelqu’un peut m'aider ?

VB:
Sub Verrouille()
  
Dim Cel As Range
Dim Plage As Range
Dim Mg As String, TB
    With Sheets("METRE")
        Set Plage = .Range("A1:J20")
       .Unprotect
        For Each Cel In Plage
            Mg = Cel.MergeArea.Address
            TB = Split(Mg, ":")
                If .Range(TB(0)).Value <> "" Then
                .Range(Mg).Locked = False
            Else
                .Range(Mg).Locked = True
            End If
        Next Cel
        .Protect
    End With
End Sub

Merci d'avance,
Benoît
 

Pièces jointes

  • METRE.xlsm
    17 KB · Affichages: 5
Solution
Donc pour les cellules non vides si True alors True, donc si False alors false, donc le mieux est de ne pas toucher aux cellules non vides :
VB:
Sub Verrouille()
Dim Cel As Range
Dim Plage As Range
Dim Mg As String, TB
    With Sheets("METRE")
        Set Plage = .Range("A1:J20")
       .Unprotect
        For Each Cel In Plage
            Mg = Cel.MergeArea.Address
            TB = Split(Mg, ":")
            If .Range(TB(0)).Value = "" Then .Range(Mg).Locked = True
        Next Cel
        .Protect
    End With
End Sub

sylvanu

XLDnaute Barbatruc
Supporter XLD
Bonjour Tioneb,
Peut être en rajoutant une condition, on déverrouille que si cellule non vide et non verrouillée.
VB:
Sub Verrouille()
Dim Cel As Range
Dim Plage As Range
Dim Mg As String, TB
    With Sheets("METRE")
        Set Plage = .Range("A1:J20")
       .Unprotect
        For Each Cel In Plage
            Mg = Cel.MergeArea.Address
            TB = Split(Mg, ":")
            If .Range(TB(0)).Value <> "" Then
                If .Range(Mg).Locked = False Then .Range(Mg).Locked = True
            Else
                .Range(Mg).Locked = True
            End If
        Next Cel
        .Protect
    End With
End Sub
 

sylvanu

XLDnaute Barbatruc
Supporter XLD
Donc pour les cellules non vides si True alors True, donc si False alors false, donc le mieux est de ne pas toucher aux cellules non vides :
VB:
Sub Verrouille()
Dim Cel As Range
Dim Plage As Range
Dim Mg As String, TB
    With Sheets("METRE")
        Set Plage = .Range("A1:J20")
       .Unprotect
        For Each Cel In Plage
            Mg = Cel.MergeArea.Address
            TB = Split(Mg, ":")
            If .Range(TB(0)).Value = "" Then .Range(Mg).Locked = True
        Next Cel
        .Protect
    End With
End Sub
 

Tioneb_h

XLDnaute Nouveau
Donc pour les cellules non vides si True alors True, donc si False alors false, donc le mieux est de ne pas toucher aux cellules non vides :
VB:
Sub Verrouille()
Dim Cel As Range
Dim Plage As Range
Dim Mg As String, TB
    With Sheets("METRE")
        Set Plage = .Range("A1:J20")
       .Unprotect
        For Each Cel In Plage
            Mg = Cel.MergeArea.Address
            TB = Split(Mg, ":")
            If .Range(TB(0)).Value = "" Then .Range(Mg).Locked = True
        Next Cel
        .Protect
    End With
End Sub
tiptop merci
 

Discussions similaires

Réponses
2
Affichages
140
Réponses
6
Affichages
125

Statistiques des forums

Discussions
312 106
Messages
2 085 352
Membres
102 871
dernier inscrit
Maïmanko