[VBA]Les macros événementielles

FAQ XLD

XLDnaute Nouveau
Cette rubrique sera alimentée j'espère régulièrement. Voici pour débuter.
_____________________________________________________________

Pour tous les codes à venir il faut

1 - Ouvrir VBE (Alt + F11)

2 - Se placer sur le projet VBA du classeur
C'est du style : VBAProject(Classeur1)
Si la fenêtre des projets n'est pas visible, il suffit de cliquer sur le menu Affichage > Explorateur de projets.

3 - Dans les exemples donnés, il y aura juste un affichage d'une msgbox
_____________________________________________________________

1 - Evènements du classeur
Le code doit se trouver dans le "ThisWorkbook"

1.1 - A l'ouverture du classeur

Code:
Private Sub Workbook_Open()
MsgBox "Merci d’avoir ouvert ce fichier"
End Sub
1.2 - A la fermeture du classeur

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
MsgBox "A BIENTOT, merci d’avoir consulté le fichier"
End Sub
____________________________________________________________

2 - Evènements d'une feuille
Le code doit se trouver dans le code de la feuille concernée

2.1 - Quand on clique sur la cellule "A1" uniquement

Code:
[COLOR=black]Private Sub Worksheet_SelectionChange(ByVal Target As Range)[/COLOR]
[COLOR=black]If Not Application.Intersect(Target, Range("A1")) Is Nothing Then[/COLOR]
[COLOR=black]MsgBox "Click on " & Target.Address[/COLOR]
[COLOR=black]End If[/COLOR]
[COLOR=black]End Sub[/COLOR]
2.2 - Quand on clique n'importe où dans la colonne "A" uniquement

Code:
[COLOR=black]Private Sub Worksheet_SelectionChange(ByVal Target As Range)[/COLOR]
[COLOR=black]If Not Application.Intersect(Target, Range("A:A")) Is Nothing Then[/COLOR]
[COLOR=black]MsgBox "Click on " & Target.Address[/COLOR]
[COLOR=black]End If[/COLOR]
[COLOR=black]End Sub[/COLOR]
2.3 - Quand on clique n'importe où dans les colonne "A" à "E" uniquement

Code:
[COLOR=black]Private Sub Worksheet_SelectionChange(ByVal Target As Range)[/COLOR]
[COLOR=black]If Not Application.Intersect(Target, Range("A:E")) Is Nothing Then[/COLOR]
[COLOR=black]MsgBox "Click on " & Target.Address[/COLOR]
[COLOR=black]End If[/COLOR]
[COLOR=black]End Sub[/COLOR]
2.4 - Quand on clique n'importe où dans la Plage de "A1 à A10" uniquement

Code:
[COLOR=black]Private Sub Worksheet_SelectionChange(ByVal Target As Range)[/COLOR]
[COLOR=black]If Not Application.Intersect(Target, Range("A1:A10")) Is Nothing Then[/COLOR]
[COLOR=black]MsgBox "Click on " & Target.Address[/COLOR]
[COLOR=black]End If[/COLOR]
[COLOR=black]End Sub[/COLOR]
2.5 - Quand on clique n'importe où dans plusieurs plages non-adjacentes

Code:
[COLOR=black]Private Sub Worksheet_SelectionChange(ByVal Target As Range)[/COLOR]
[COLOR=black]If Not Application.Intersect(Target, Range("A1:A12, D4:D10, D20, D22, D55, E1:E12")) Is Nothing Then[/COLOR]
[COLOR=black]MsgBox "Click on " & Target.Address[/COLOR]
[COLOR=black]End If[/COLOR]
[COLOR=black]End Sub[/COLOR]
2.6 - Quand on clique n'importe où dans plusieurs colonnes non-contigues

Code:
[COLOR=black]Private Sub Worksheet_SelectionChange(ByVal Target As Range)[/COLOR]
[COLOR=black]If Not Application.Intersect(Target, Range("A:A, C:C, E:E")) Is Nothing Then[/COLOR]
[COLOR=black]MsgBox "Click on " & Target.Address[/COLOR]
[COLOR=black]End If[/COLOR]
[COLOR=black]End Sub [/COLOR]
2.7 - Quand on clique n'importe où dans la ligne 1

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Application.Intersect(Target, Rows(1)) Is Nothing Then
MsgBox "Click on " & Target.Address
End If
End Sub
2.8 - Quand on clique n'importe où entre les lignes 1 et 3

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Application.Intersect(Target, Range(Rows(1), Rows(3))) Is Nothing Then
MsgBox "Click on " & Target.Address
End If
End Sub
2.9 - Quand on clique n'importe où dans dans les lignes 1 ou 3 uniquement (pas la 2)

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Application.Intersect(Target, Range(Rows(1), Rows(3))) Is Nothing Then
If Application.Intersect(Target, Rows(2)) Is Nothing Then
MsgBox "Click on " & Target.Address
End If
End If
End Sub
_____________________________________________________________
Merci à Brigitte, Skoobi et Thierry pour leurs participations
 
Dernière modification par un modérateur:

Discussions similaires

Statistiques des forums

Discussions
312 239
Messages
2 086 495
Membres
103 234
dernier inscrit
matteo75654548