Commande Unload et Show pour initialiser des CommandButton ...

Christian0258

XLDnaute Accro
Bonjour à tout le forum,
J'ai un USF avec 21 CommandButtons. J'ai des *Labels, pour certains CommandButtons, qui doivent être *initialisés. J'ai dans "Modules de Classe" sous ClasseBoutons cette macro Unload et Show. Mais cette ouverture et fermeture est un peu pénible pour les yeux.
Une dizaines de ces CommandButton (n°6, 7, 14, 15, 16, 17, 18, 19, 20, 21) ne sont pas concernées par l'initialisation.
Comment faire pour que l'USF se ferme/ouvre uniquement sous l'action des autres CommandButtons concernées...?

Public WithEvents GrBoutons As Msforms.CommandButton
Private Sub GrBoutons_Click()
Selection.Interior.Color = GrBoutons.BackColor
Selection.Font.Color = GrBoutons.ForeColor
If IsNumeric(Application.Search(" ", GrBoutons.Caption, 1)) Then
Selection.Value = Left(GrBoutons.Caption, Application.Search(" ", GrBoutons.Caption, 1) - 1)
Else
Selection.Value = GrBoutons.Caption
End If
Unload UserForm1
UserForm1.Show
End Sub

Merci pour votre aide.
Bien amicalement,
Christian
 

mutzik

XLDnaute Barbatruc
bonjour,

quand j'utilise les classes, je teste si le controle est un bouton ou une combobox ou ...
j'utilise également la propriété Tag (à utiliser comme on veut) pour différencier les groupes de command bouton

comme cela, je n'initialise ou change les propriétés (ou ...) que des boutons appartenant à ce Tag et à cette classe bien précise

concernant l'initialise, je crée toujours une sub à part pour initialiser mon userform
je place ensuite le nom de cette sub dans mon initialise
ce qui permet, en cours de traitement de n'initialiser que cette partie en appelant la sub qui va bien sans ré-initialiser mon userform en entier avec tous les controles et variables et remplissage de listes, de combo (chaque opération dans une sub à part)
 

Roland_M

XLDnaute Barbatruc
re

salut mutzik !

évidemment tout dépend du code dans l'initialize sinon une astuce simple

dans ton userform tu déplaces tout ce qu'il y a dans une routine par exemple
Public Sub InitUserForm1()
...
End Sub

et dans Private Sub UserForm_Initialize() tu appelles cette routine InitUserForm1
Private Sub UserForm_Initialize()
InitUserForm1
End Sub

et pour l'appel: UserForm1.InitUserForm1
 

Christian0258

XLDnaute Accro
Re, le forum, Bertrand, mutzik

J'ai lu avec attention vos recommandations et explications, mais je ne suis pas assez compétent pour les appliquer.
J'ai ceci dans mon USF

Private Sub UserForm_Initialize()
For I = 1 To 21
Me("CommandButton" & I).BackColor = Sheets("Planning").Cells(I, 509).Interior.Color
Me("CommandButton" & I).ForeColor = Sheets("Planning").Cells(I, 509).Font.Color
Me("CommandButton" & I).Caption = Sheets("Planning").Cells(I, 509).Text
Set Btn(I).GrBoutons = Me("commandbutton" & I)
Next I

Label5.Caption = Sheets("Planning").Range("SP6").Value
Label6.Caption = Sheets("Planning").Range("SP3").Value
Label7.Caption = Sheets("Planning").Range("SP15").Value
Label8.Caption = Sheets("Planning").Range("SP16").Value
Label9.Caption = Sheets("Planning").Range("SP7").Value
Label10.Caption = Sheets("Planning").Range("SP14").Value
Label11.Caption = Sheets("Planning").Range("SP17").Value
Label12.Caption = Sheets("Planning").Range("SP20").Value
Label13.Caption = Sheets("Planning").Range("SP19").Value
Label14.Caption = Sheets("Planning").Range("SP18").Value
Label15.Caption = Sheets("Planning").Range("SP21").Value

End Sub

A vous lire...
Bien à vous,
Christian
 

Roland_M

XLDnaute Barbatruc
re

dans ton userfom1 :

Private Sub UserForm_Initialize()
InitUserForm1
End Sub

Public Sub InitUserForm1()
For I = 1 To 21
Me("CommandButton" & I).BackColor = Sheets("Planning").Cells(I, 509).Interior.Color
Me("CommandButton" & I).ForeColor = Sheets("Planning").Cells(I, 509).Font.Color
Me("CommandButton" & I).Caption = Sheets("Planning").Cells(I, 509).Text
Set Btn(I).GrBoutons = Me("commandbutton" & I)
Next I

Label5.Caption = Sheets("Planning").Range("SP6").Value
Label6.Caption = Sheets("Planning").Range("SP3").Value
Label7.Caption = Sheets("Planning").Range("SP15").Value
Label8.Caption = Sheets("Planning").Range("SP16").Value
Label9.Caption = Sheets("Planning").Range("SP7").Value
Label10.Caption = Sheets("Planning").Range("SP14").Value
Label11.Caption = Sheets("Planning").Range("SP17").Value
Label12.Caption = Sheets("Planning").Range("SP20").Value
Label13.Caption = Sheets("Planning").Range("SP19").Value
Label14.Caption = Sheets("Planning").Range("SP18").Value
Label15.Caption = Sheets("Planning").Range("SP21").Value
End Sub

et pour l'appel afin de reinit: UserForm1.InitUserForm1


EDIT:
si ça pose problème avec la boucle Set Btn(I).GrBoutons = Me("commandbutton" & I)
afin que ceci ne s'exécute pas deux fois si c'est pas nécessaire,
il suffit de mettre une variable au début du module userform1

Dim UserfInit as boolean

et ici
Private Sub UserForm_Initialize()
UserfInit = False: InitUserForm1: UserfInit = True
End Sub

puis tester UserfInit dans InitUserForm1
Public Sub InitUserForm1()
if UserfInit = False then
For I = 1 To 21
Me("CommandButton" & I).BackColor = Sheets("Planning").Cells(I, 509).Interior.Color
Me("CommandButton" & I).ForeColor = Sheets("Planning").Cells(I, 509).Font.Color
Me("CommandButton" & I).Caption = Sheets("Planning").Cells(I, 509).Text
Set Btn(I).GrBoutons = Me("commandbutton" & I)
Next I
end if
. . .
. . .
. . .
 
Dernière édition:

Roland_M

XLDnaute Barbatruc
re

milles excuses mutzik j'avais pas vu ta signature Bertrand ! j'ai cru qu'il confondait et qu'il m'appelait Bertrand !
je dois pas être tout à fait réveillé ! il serait temps pourtant !

et effectivement c'est ce que tu préconisais aussi !
je travaille de la même façon, et comme tu dis:
Nous sommes tout à fait sur la même longueur d'onde !
 
Dernière édition:

Statistiques des forums

Discussions
312 837
Messages
2 092 660
Membres
105 482
dernier inscrit
Eric.FKF