IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

autorisation/ interdiction conditionnelle d'écriture


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2008
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 66
    Par défaut autorisation/ interdiction conditionnelle d'écriture
    Bonjour tout le monde,

    Afin d'éviter tout problème, j'essaie d'interdire l'écriture dans une cellule excel si ma combobox est vide.
    Pour l'instant, la structure du code que j'ai est le suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    if ComboBox1.Value="" then
        Worksheets("Feuil1").Range("B3")=""
    End If
    Si quelqu'un peut me donner un coup de main, je suis preneur.

    Cordialement.

  2. #2
    Membre éclairé Avatar de tomy7
    Profil pro
    Étudiant
    Inscrit en
    Janvier 2008
    Messages
    540
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2008
    Messages : 540
    Par défaut
    tu verrouilles ta cellule en faisant clic droit > format cell >protection> locked

    puis dans ton code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    if ComboBox1.Value="" then
       Worksheets("Feuil1").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End If
    tu peux regarder ici aussi:

    http://www.developpez.net/forums/sho...d.php?t=506576

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2008
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 66
    Par défaut
    Apparemment, ce code permet de protéger TOUTES les cellules vérouillées.

    Le problème, c'est que j'ai plusieurs combobox indépendantes qui autorisent ou interdisent une case différente.

    Exemples :

    Combobox1->"B3"
    Combobox1->"B4"
    Combobox1->"B5"
    Combobox1->"B5"

    Tu vois ce que je veux dire?

  4. #4
    Membre émérite
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    633
    Détails du profil
    Informations personnelles :
    Âge : 57
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 633
    Par défaut
    bonjour

    essaye avec

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
        With Worksheets("Feuil1")
          .Cells.Locked = False
          Cellules_a_proteger = "A1,A3,C5"
          .Range(Cellules_a_proteger).Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2008
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 66
    Par défaut
    ok, je vais essayer.

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2008
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 66
    Par défaut
    Avec ces indications, j'ai essayé de monter le code suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    Private Sub ListBox1_Change()
     
    If ListBox1.Selected(0) = False Then
        ComboBox5.Visible = False
        ComboBox5.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G5,J5") = ""
          .Range("G5,J5").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox5.Visible = True
        ComboBox5.Enabled = True
        Worksheets("Feuil1").Range("G5").Locked = False
    End If
    If ListBox1.Selected(1) = False Then
        ComboBox5.Visible = False
        ComboBox5.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G6,J6") = ""
          .Range("G6,J6").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox6.Visible = True
        ComboBox6.Enabled = True
        Worksheets("Feuil1").Range("G6").Locked = False
    End If
    If ListBox1.Selected(2) = False Then
        ComboBox7.Visible = False
        ComboBox7.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G7,J7") = ""
          .Range("G7,J7").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox7.Visible = True
        ComboBox7.Enabled = True
        Worksheets("Feuil1").Range("G7").Locked = False
    End If
    If ListBox1.Selected(3) = False Then
        ComboBox8.Visible = False
        ComboBox8.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G8,J8") = ""
          .Range("G8,J8").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox8.Visible = True
        ComboBox8.Enabled = True
        Worksheets("Feuil1").Range("G8").Locked = False
    End If
    If ListBox1.Selected(4) = False Then
        ComboBox9.Visible = False
        ComboBox9.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G9,J9") = ""
          .Range("G9,J9").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox9.Visible = True
        ComboBox9.Enabled = True
        Worksheets("Feuil1").Range("G9").Locked = False
    End If
    If ListBox1.Selected(5) = False Then
        ComboBox10.Visible = False
        ComboBox10.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G10,J10") = ""
          .Range("G10,J10").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox10.Visible = True
        ComboBox10.Enabled = True
        Worksheets("Feuil1").Range("G10").Locked = False
    End If
    If ListBox1.Selected(6) = False Then
        ComboBox11.Visible = False
        ComboBox11.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G11,J11") = ""
          .Range("G11,J11").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox11.Visible = True
        ComboBox11.Enabled = True
        Worksheets("Feuil1").Range("G11").Locked = False
    End If
    If ListBox1.Selected(7) = False Then
        ComboBox12.Visible = False
        ComboBox12.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G12,J12") = ""
          .Range("G12,J12").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox12.Visible = True
        ComboBox12.Enabled = True
        Worksheets("Feuil1").Range("G12").Locked = False
    End If
    If ListBox1.Selected(8) = False Then
        ComboBox13.Visible = False
        ComboBox13.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G13,J13") = ""
          .Range("G13,J13").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox13.Visible = True
        ComboBox13.Enabled = True
        Worksheets("Feuil1").Range("G15").Locked = False
    End If
    If ListBox1.Selected(9) = False Then
        ComboBox14.Visible = False
        ComboBox14.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G14,J14") = ""
          .Range("G14,J14").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox14.Visible = True
        ComboBox14.Enabled = True
        Worksheets("Feuil1").Range("G14").Locked = False
    End If
    If ListBox1.Selected(10) = False Then
        ComboBox15.Visible = False
        ComboBox15.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G15,J15") = ""
          .Range("G15,J15").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox15.Visible = True
        ComboBox15.Enabled = True
        Worksheets("Feuil1").Range("G15").Locked = False
    End If
    If ListBox1.Selected(11) = False Then
        ComboBox16.Visible = False
        ComboBox16.Enabled = False
     
        With Worksheets("Feuil1")
          .Cells.Locked = False
          .Range("G16,J16") = ""
          .Range("G16,J16").Locked = True
          .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End With
     
    Else
        ComboBox16.Visible = True
        ComboBox16.Enabled = True
        Worksheets("Feuil1").Range("G16").Locked = False
     
    End If
     
     
    End Sub
    Mais quand je l'active, il me met : "Impossible de définir la propriété Locked de la classe Range" alors que j'ai veillé que les cellules soit vérouillées comme avait dit Tomy7.
    Et si au passage, quelqu'un saurait comment je pourrais alléger ce programme, je suis preneur.

    Cordialement.

  7. #7
    Membre émérite
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    633
    Détails du profil
    Informations personnelles :
    Âge : 57
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 633
    Par défaut
    Bonjour

    J’ai oublié une précision, tu ne peux modifier Locked que si la feuille est déprotégé donc il ne faut utiliser Protect qu’en début et fin de ton Sub.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    Private Sub ListBox1_Change()
      Worksheets("Feuil1").Protect DrawingObjects:=False, Contents:= False, Scenarios:= False
      ‘…
      ‘…
      ‘Tout ton code
      ‘…
      ‘…
      Worksheets("Feuil1").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End Sub
    Si j’ai un peu de temps je verrais s’il y a moyen d’optimiser

Discussions similaires

  1. [WD20] Table : interdiction conditionnelle de quitter la ligne sélectionnée.
    Par xavier.ninane dans le forum WinDev
    Réponses: 15
    Dernier message: 17/03/2015, 10h20
  2. [Tree][Drag & Drop] Autorisation / Interdiction
    Par nikko49 dans le forum Ext JS / Sencha
    Réponses: 1
    Dernier message: 02/11/2013, 11h53
  3. Pb pour autoriser l'écriture d'un fichier
    Par slimounet dans le forum ASP
    Réponses: 6
    Dernier message: 25/04/2006, 16h09
  4. [W3C] Ne pas autoriser l'écriture dans un champ INPUT
    Par kerlann dans le forum Balisage (X)HTML et validation W3C
    Réponses: 7
    Dernier message: 24/04/2006, 10h22
  5. [VBA-E] ré-autoriser écriture sur TextBox
    Par repié dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 03/03/2006, 15h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo