... le calendrier s'affiche à chaque fois qu'on selectionne une cellule au format date, ce qui est un poil énervant (quand on n'en n'a pas besoin)
Il est possible d'afficher ou masquer le bouton calendrier dans la barre d'outils standard, par contre, il n'est effectivement pas prévu de pouvoir le déplacer dans les barres d'outils.Guiv à dit:... le bouton "calendrier" se place dans la barre d'outils standard, et je le voudrais dans ma barre perso...
Option Explicit
Dim Usf As Object
Sub LancementProcedure()
Dim X As Object
Dim NomMonthView As String
NomMonthView = "MonthView1"
'Lance la procédure de création du userform et du contrôle MonthView
Set X = UserForm_Et_MonthView_Dynamique(NomMonthView)
'Affichage userform
X.Show
'Suppression du userform après la fermeture
ThisWorkbook.VBProject.VBComponents.Remove Usf
Set Usf = Nothing
End Sub
Function UserForm_Et_MonthView_Dynamique(NomObjet As String) As Object
Dim Obj As Object
Dim j As Integer
'Création UserForm
Set Usf = ThisWorkbook.VBProject.VBComponents.Add(3)
With Usf
.Properties("Caption") = "Mon calendrier"
.Properties("Width") = 135
.Properties("Height") = 140
End With
'Création du contrôle MonthView
Set Obj = Usf.Designer.Controls.Add("MSComCtl2.MonthView.2")
With Obj
.Left = 0: .Top = 0: .Width = 150: .Height = 140
.Name = NomObjet
.ForeColor = &HC000C0
.TitleBackColor = &HC000C0
End With
'Ajout de la procédure évènementielle DateClick du contrôle MonthView
With Usf.CodeModule
j = .CountOfLines
.insertlines j + 1, "Sub " & NomObjet & "_DateClick(ByVal DateClicked As Date)"
'Insère la date dans la cellule active
.insertlines j + 2, " ActiveCell = DateClicked"
'Option pour refermer l'userform après l'insertion de la date.
'.insertlines j + 3, " Unload Me"
.insertlines j + 4, "End Sub"
End With
VBA.UserForms.Add (Usf.Name)
Set UserForm_Et_MonthView_Dynamique = UserForms(UserForms.Count - 1)
End Function
Option Explicit
Dim Usf As Object
Sub LancementProcedure()
Dim X As Object
Dim NomdtPicker As String
NomdtPicker = "DtPicker1"
Set X = UserForm_Et_DataPicker_Dynamique(NomdtPicker)
X.Show
ThisWorkbook.VBProject.VBComponents.Remove Usf
Set Usf = Nothing
End Sub
Function UserForm_Et_DataPicker_Dynamique(NomObjet As String) As Object
Dim Obj As Object
Dim j As Integer
Set Usf = ThisWorkbook.VBProject.VBComponents.Add(3)
With Usf
.Properties("Caption") = "Mon calendrier"
.Properties("Width") = 130
.Properties("Height") = 40
End With
Set Obj = Usf.Designer.Controls.Add("MSComCtl2.DTPicker.2")
With Obj
.Left = 0: .Top = 0: .Width = 130: .Height = 20
.Name = NomObjet
.CalendarBackColor = &HFF00FF
End With
With Usf.CodeModule
j = .CountOfLines
.insertlines j + 1, "Sub " & NomObjet & "_Change()"
.insertlines j + 2, " ActiveCell.Value = Format(DateSerial(Year(" _
& NomObjet & "), Month(" & NomObjet & "), Day(" _
& NomObjet & ")), " & Chr(34) & "dd mmmm yyyy" & Chr(34) & ")"
'Option pour refermer l'userform après l'insertion de la date.
'.insertlines j + 3, " Unload Me"
.insertlines j + 4, "End Sub"
End With
VBA.UserForms.Add (Usf.Name)
Set UserForm_Et_DataPicker_Dynamique = UserForms(UserForms.Count - 1)
End Function
L'ocx Windows common control-2 6.0 est distribué avec les packs développeurs et vb6.Pour essayer ton code, quelle version de Windows et D'Excel faut-il avoir?
'Cet exemple est placé dans le module objet du classeur et utilise
'l'évènement Workbook_Open.
'De cette manière, la barre est créée automatiquement lors de
'l'ouverture du fichier
Private Sub Workbook_Open()
Dim CmdBar As CommandBar
Dim Bouton As CommandBarButton
'Création de la barre d'outils nommée 'MaBarrePerso'
Set CmdBar = Application.CommandBars _
.Add(Name:="MaBarrePerso", Position:=msoBarTop, Temporary:=True)
'Ajout d'un bouton dans la barre d'outils
Set Bouton = CmdBar.Controls.Add(Type:=msoControlButton)
With Bouton
'Définit "l'image" qui va s'afficher sur le bouton
.FaceId = 33
'Définit quelle macro est associée au bouton.
'Cette macro sera lancée à chaque fois que vous cliquez sur le bouton.
.OnAction = "LancementProcedure"
End With
CmdBar.Visible = True
End Sub
'Ce deuxième code utilise l'évènement Workbook_BeforeClose et
'supprime la barre personnelle lors de la fermeture du classeur.
'Rappel:
'Dans Excel2007, les menus et barres d'outils personnels sont stockés
'dans l'onglet "Complément".
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.CommandBars("MaBarrePerso").Delete
End Sub
'--- Dans un module Standard ---
Option Explicit
Dim Usf As Object
Sub LancementProcedure()
Dim X As Object
Dim NomMonthView As String
NomMonthView = "MonthView1"
'Lance la procédure de création du userform et du contrôle MonthView
Set X = UserForm_Et_MonthView_Dynamique(NomMonthView)
'Affichage userform
X.Show
'Suppression du userform après la fermeture
ThisWorkbook.VBProject.VBComponents.Remove Usf
Set Usf = Nothing
End Sub
Function UserForm_Et_MonthView_Dynamique(NomObjet As String) As Object
Dim Obj As Object
Dim j As Integer
'Création UserForm
Set Usf = ThisWorkbook.VBProject.VBComponents.Add(3)
With Usf
.Properties("Caption") = "Mon calendrier"
.Properties("Width") = 135
.Properties("Height") = 140
End With
'Création du contrôle MonthView
Set Obj = Usf.Designer.Controls.Add("MSComCtl2.MonthView.2")
With Obj
.Left = 0: .Top = 0: .Width = 150: .Height = 140
.Name = NomObjet
.ForeColor = &HC000C0
.TitleBackColor = &HC000C0
'.MinDate = CDate("01/01/2007")
'.MaxDate = CDate("31/12/2007")
End With
'Ajout de la procédure évènementielle DateClick du contrôle MonthView
With Usf.CodeModule
j = .CountOfLines
.insertlines j + 1, "Sub " & NomObjet & "_DateClick(ByVal DateClicked As Date)"
'Sélectionne la feuille correpondant au numéro de semaine
'Cet exemple nécessite que le classeur contienne 52 feuilles.
.insertlines j + 2, " Worksheets( " & NomObjet & ".Week ).Activate"
'Option pour refermer l'userform après l'insertion de la date.
.insertlines j + 3, " Unload Me"
.insertlines j + 4, "End Sub"
End With
VBA.UserForms.Add (Usf.Name)
Set UserForm_Et_MonthView_Dynamique = UserForms(UserForms.Count - 1)
End Function