Bloquer cellule VBA

Bonjour,

j'ai besoin d'aide svp !!!!

J'ai 3 cellules. Par exemple :

A1

A2

A3

dans lesquelles on va retrouver des lettres.

Si 2 sont remplies, je veux bloquer la troisième, en gros qu'on puisse rien écrire dedans.

Est ce que quelqu'un pourrait me donner des pistes pour y arriver ?

Merci !!!

Bonjour

Un truc rapide

A placer dans le module de la feuille concernée

Private Sub Worksheet_Change(ByVal Target As Range)
  If Application.CountA(Range("A1:A3")) = 3 Then
    Application.Undo
  End If
End Sub

très très débutant en VBA,

sympat ce code pour faire des formulaires où il y aurait qu'un choix possible...

et si je veux le faire de A1 à C1 et sur plusieurs lignes ? j'ai essayé ce code mais ça ne marche pas

Private Sub Worksheet_Change(ByVal Target As Range)
  If Application.CountA(Range("A1:C1")) = 2 Then
    Application.Undo
    End If
  If Application.CountA(Range("B1:C1")) = 2 Then
    Application.Undo
  End If
End Sub

Bonjour

Je ne sais pas si c'est la meilleure solution

Francky69100 a écrit :

j'ai essayé ce code mais ça ne marche pas

Cela fait ce que tu lui as demandé

Repenses à ce que tu veux faire

Pour moi la 2ème condition est inutile : Il ne pourra y avoir qu'une seule valeur en A1:C1

pourquoi la chose suivante ne marche pas ??

Private Sub Worksheet_Change(ByVal Target As Range)

If Application.CountA(Range("C4:C6")) = 3 Then

Application.Undo

End If

If Application.CountA(Range("C9:C18")) = 5 Then

Application.Undo

End If

End Sub

oups, excuse-moi Banzaî,

ce code marche bien, mais si je veux aller à la ligne 20 faut que je tape le code pour chaque ligne :

Private Sub Worksheet_Change(ByVal Target As Range)
  If Application.CountA(Range("A1:C1")) = 2 Then
    Application.Undo
    End If
  If Application.CountA(Range("A2:C2")) = 2 Then
    Application.Undo
  End If
End Sub

Francky


Pour moi ce code fonctionne très bien, combien veux-tu de cellule qui se remplissent entre C9 et C18

Private Sub Worksheet_Change(ByVal Target As Range)
  If Application.CountA(Range("C4:C6")) = 3 Then
    Application.Undo
    End If
  If Application.CountA(Range("C9:C18")) = 5 Then
    Application.Undo
  End If
End Sub

Francky

jai fermé et ré-ouvert excel ca marche merciiiiiiiiiiiiiiiiiiiiiiii


Comment tu fais pour ajouter une deuxième conséquence à ta condition :

Private Sub Worksheet_Change(ByVal Target As Range)

If Application.CountA(Range("C4:C6")) > 2 Then Application.Undo

Then MsgBox "Attention"

End Sub

essaie

Private Sub Worksheet_Change(ByVal Target As Range)
  If Application.CountA(Range("A1:C1")) = 2 Then
    MsgBox "Attention"
    Application.Undo
    End If
  If Application.CountA(Range("A2:C2")) = 2 Then
  MsgBox "Attention"
    Application.Undo
  End If
End Sub

Banzaî j'attends toujours ma réponse, je suis très débutant en VBA

Francky

Bonsoir

Francky69100 a écrit :

Banzaî j'attends toujours ma réponse

Ca c'est fait pour me motiver

ok, je vais faire des essais...

Francky

ok, une première tentative qui fonctionne pas car toute la plage est concerné !

Private Sub Worksheet_Change(ByVal Target As Range)
If Application.CountA(Range("A2:C2", "A4:C4")) = 2 Then
  MsgBox "Attention"
    Application.Undo
  End If
End Sub

un coup de pouce ! je t'ai dis que j'étais débutant...

Francky

Bonsoir

Je ne sais pas ce que tu veux faire

Si je comprends : Dans les zones A2:C2 et A4::C4 il ne peut y avoir qu'une seule cellule de notée

Private Sub Worksheet_Change(ByVal Target As Range)
  If Application.CountA(Range("A2:C2,A4:C4")) = 2 Then
    MsgBox "Attention"
    Application.Undo
  End If
End Sub

non, je vais essayé d'être plus clair :

je voudrais que ce code :

Private Sub Worksheet_Change(ByVal Target As Range)
  If Application.CountA(Range("A2:C2")) = 2 Then
    MsgBox "Attention"
    Application.Undo
  End If
End Sub
 

puisse ce reporduire sur 10 lignes par exemple, sans à avoir à le recopier 10 fois... (une cellule peut être saisi entre A2 et C2, puis 1 Cellule peut être saisi entre A3 et C3...)

j''espère avoir été un peu plus clair,

Francky

Bonsoir

Essayes ceci

Private Sub Worksheet_Change(ByVal Target As Range)
  If Application.CountA(Range("A" & Target.Row).Resize(1, 3)) = 2 And Target.Row < 11 Then
    MsgBox "Attention"
    Application.Undo
  End If
End Sub

bonsoir Banzaï,

non, oa n'a pas l'aire de fonctionner, mais ce n'est pas grave, j'essai d'apprendre petit à petit le VBA.

en fait c'était pour éviter le code cit-dessous.

Private Sub Worksheet_Change(ByVal Target As Range)
  If Application.CountA(Range("A1:C1")) = 2 Then
    MsgBox "Attention"
    Application.Undo
    End If
    If Application.CountA(Range("A2:C2")) = 2 Then
    MsgBox "Attention"
    Application.Undo
    End If
  If Application.CountA(Range("A3:C3")) = 2 Then
  MsgBox "Attention"
    Application.Undo
    End If
    If Application.CountA(Range("A4:C4")) = 2 Then
    MsgBox "Attention"
    Application.Undo
    End If
    If Application.CountA(Range("A5:C5")) = 2 Then
    MsgBox "Attention"
    Application.Undo
    End If
    If Application.CountA(Range("A6:C6")) = 2 Then
    MsgBox "Attention"
    Application.Undo
    End If
    If Application.CountA(Range("A7:C7")) = 2 Then
    MsgBox "Attention"
    Application.Undo
    End If
    If Application.CountA(Range("A8:C8")) = 2 Then
    MsgBox "Attention"
    Application.Undo
    End If
    If Application.CountA(Range("A9:C9")) = 2 Then
    MsgBox "Attention"
    Application.Undo
    End If
End Sub

Merci pour tes effort et Mokia tu devrais valider ce post car tu as eu ta réponse...

bonne nuit

Francky

Bonjour

Francky69100 a écrit :

non, oa n'a pas l'aire de fonctionner,

Ah si c'est important

J'ai fait des test et pas de soucis

Sur les 10 premières lignes on ne peut saisir qu'une donnée dans une seule colonne entre A et C

Si quelqu'un peut essayer

Merci

Mea-culpa Banzaï,

j'ai rééssayé ce matin et tout marche, je crois que comme je travaillais sur un fichier ouvert depuis un certain temps et qu'il y avait déjà des donnés sur la feuille, il prennait pas le nouveau code en compte.

c'est génial, je vais essayé de l'adapter.

Francky

PS: Mokia valide !

comment tu fais pour appliquer la meme macro a différentes colonnes

jai tenté un truc mais ca marche pas

je veux travailler sur les 3 cellules D3/D4/D5; E3/E4/E5, F3/F4/F5 par exemple

For i = 1 To 3

If Application.CountA(Range("Cells(3,i).Value:Cells(5,i).Value")) > 2 Then

Application.Undo

MsgBox "STOP !"

End If

Next i

Bonjour

C'était presque ça

Les guillemets en trop

La boucle c'est de 4 ( D ) à 6 ( F )

Les .Value : Je ne sais pas

A voir

Private Sub Worksheet_Change(ByVal Target As Range)
Dim I As Integer

  For I = 4 To 6
    ' Les 2 lignes suivantes désignent la même plage
    'If Application.CountA(Cells(3, I).Resize(3, 1)) > 2 Then
    If Application.CountA(Range(Cells(3, I), Cells(5, I))) > 2 Then
      MsgBox "Attention"
      Application.Undo
    End If
  Next I
End Sub

merci beaucoup beaucoup

cest hyper cool de ta part de mavoir aidé

merci encore

Rechercher des sujets similaires à "bloquer vba"