Macro affectée à un menu contextuel
Bonjour,
je crée un menu contextuel quand je fais un clic droit sur une cellule, et ce menu comporte des boutons qui déclenchent des procédures ... seulement le pb c'est qu'il les déclenche toujours 2 fois.
Code:
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
| Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
'...
' Création du menu contextuel
On Error Resume Next
Application.CommandBars("PlanningBarre").Delete
If Err.Number <> 0 Then
Err.Clear
End If
On Error GoTo 0
Dim CommandBarPlanning As CommandBar
Dim NouveauBouton As CommandBarControl
Dim NouveauBouton2 As CommandBarControl
Set CommandBarPlanning = Application.CommandBars.Add("PlanningBarre", msoBarPopup, , True)
'Remplacements
Dim Plage As Range
Dim Cell As Range
' Init de variable qui génère une erreur si la plage n'existe pas
On Error Resume Next
Set Plage = Worksheets("ListeDispo").Range("ListeDispo")
If Err.Number = 1004 Then
Err.Clear
Set NouveauBouton = CommandBarPlanning.Controls.Add
NouveauBouton.Caption = "Pas de remplaçant"
Else
For Each Cell In Worksheets("ListeDispo").Range("ListeDispo")
Set NouveauBouton = CommandBarPlanning.Controls.Add
NouveauBouton.Caption = Cell.Text
NouveauBouton.OnAction = "FeuillePlanning.Remplacer(""" & Cell.Text & """)"
Next Cell
End If
On Error GoTo 0
CommandBarPlanning.ShowPopup
Exit Sub |
Dans code précédent c'est la ligne NouveauBouton.OnAction = "FeuillePlanning.Remplacer(""" & Cell.Text & """)" qui affecte au control une macro qui s'appelle par exemple FeuillePlanning.Remplacer("Dupont").
ListeDispo est une plage nommée qui contient des noms ...
Code:
1 2 3 4 5
| Sub Remplacer(NomRemplacant As String)
Debug.Print NomRemplacant
End Sub |
Le problème c'est que cette macro s’exécute 2 fois, alors que si j'ai une syntaxe de type NouveauBouton.OnAction = "FeuillePlanning.RemplacerDupond" c'est a dire avec une procédure sans paramètre, celle ci n'est exécutée qu'une seule fois.
Auriez vous une idée du pourquoi comment ??? MERCI