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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| Private Sub CommandButton1_Click()
Dim Prenom As String
Dim Nom As String
Dim usf As VBComponent
Dim Ctrl As Control
'tri alphabétique des données dans la feuille 2
Range("A5:B371").Select
Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
'désactivation de l'écran pour la durée de la macro
Application.ScreenUpdating = False
'compter le nombre de personnes (de lignes) dans la feuil2
ActiveSheet.Cells(5, 2).Select
NbPers = 0
While ActiveCell.Value <> ""
NbPers = NbPers + 1
ActiveCell.Offset(1, 0).Select
Wend
'supprimer les checkbox de la userform1
'For Each Ctrl In UserForm1.Controls
' If TypeName(Ctrl) = "checkbox" Then
' CA BUGGUE ICI
' Ctrl.remove
' End If
'Next Ctrl
'intégrer les noms prénoms sous forme de checkbox
ReDim Obj(NbPers)
Set usf = ThisWorkbook.VBProject.VBComponents("UserForm1")
'autre tentative de nettoyer la userform de ses contrôles
'BUGGUE UNE FOIS SUR DEUX (histoire du mode conception/exécution
'usf.Designer.Clear
ActiveSheet.Cells(5, 2).Select
For i = 1 To NbPers
Nom = ActiveCell.Value
Prenom = ActiveCell.Offset(0, -1).Value
Set Obj(i) = usf.Designer.Controls.Add("forms.CheckBox.1")
With Obj(i)
.Left = 30
.Top = 10 + 20 * i
.Width = 150
.Height = 20
.Name = "Gars" & i
.Caption = Prenom & " " & Nom
End With
ActiveCell.Offset(1, 0).Activate
Next
'redéfinir la hauteur de la userform
With usf
.Properties("height") = 130 + 20 * NbPers
.Properties("caption") = "Sélection du personnel"
End With
'intégrer du texte figé
Set legende = usf.Designer.Controls.Add("forms.label.1")
With legende
.Left = 20
.Top = 10
.Width = 130
.Height = 20
.Caption = "Sélectionner les personnes :"
End With
'intégrer des contrôles
Set modif = usf.Designer.Controls.Add("forms.commandbutton.1")
With modif
.Left = 40
.Top = 40 + 20 * NbPers
.Width = 120
.Height = 20
.Caption = "Modifier la liste du personnel"
End With
Set Valid = usf.Designer.Controls.Add("forms.commandbutton.1")
With Valid
.Left = 50
.Top = 70 + 20 * NbPers
.Width = 30
.Height = 20
.Caption = "OK"
End With
Set annul = usf.Designer.Controls.Add("forms.commandbutton.1")
With annul
.Left = 104
.Top = 70 + 20 * NbPers
.Width = 45
.Height = 20
.Caption = "Annuler"
End With
VBA.UserForms.Add (usf.Name)
Application.ScreenUpdating = True
'revenir à la feuille 1
Sheets(1).Visible = True
Sheets(1).Activate
Sheets("liste du personnel SNM Méca").Visible = False
'faire apparaitre la userform remaniée
'comme usf.show plante, ou userform1 aussi, je n'ai trouvé que ce moyen :
' externaliser la demande dans une autre procédure
Call chaud
End Sub
Sub chaud()
UserForm1.Show
End Sub |