XL 2016 Exécuter une action sur checkbox créé

dodineau

XLDnaute Occasionnel
Bonjour à toutes et tous.
Dans mon formulaire je créé des label et des checkbox en fonction d'un nombre choisi entre 3 et 10.
Par exemple, pour 3 je créé :
Label_1 et checkbox_1
Label_2 et checkbox_2
Label_3 et checkbox_3
et enfin Label_total
Je voudrais maintenant pouvoir écrire dans le Label_total la somme des label 1 2 3 en fonction des checkbox cochés.
Je n'arrive pas à trouver la solution pour créer un événement sur les checkbox créées.
Merci pour votre aide.
A+
 
Solution
bon j'ai bricolé quelque chose
il te faut taguer tes label avec le nom du checkbox au quel il correspond et cela parce que tu construit avant le show
si tu construisais dans le activate du userform ca serait plus simple

donc voici ta sub de création + show
VB:
Sub test()
 Dim i&
              i = 1
              For k = 1 To 3
                Set Obj = Form_Conso.Controls.Add("forms.Label.1")
                With Obj
                    .Name = "Stock" & k
                    .Caption = Cells(i + k - 1, 19)
                    .Left = 1014
                    .Top = 130 + (k - 1) * 14
                    .Width = 90
                    .Height = 12
                    .Tag = "CB_" & k
                    '.BackColor = Back...

dodineau

XLDnaute Occasionnel
Bonjour et merci pour ta réponse.
Voici un bout du code qui créé les labels et checkbox.
Ce code est dans une "sheetbeforedoubleclick"
C'est une boucle en fonction d'un nombre entre 3 et 10

VB:
                Set Obj = Form_Conso.Controls.Add("forms.Label.1")
                With Obj
                    .Name = "Stock" & k
                    .Object.Caption = Cells(i + k - 1, 19)
                    .Left = 1014
                    .Top = 130 + (k - 1) * 14
                    .Width = 90
                    .Height = 12
                    .BackColor = Back
                    .Font.Name = "Tahoma"
                    .Font.Size = 10
                    .Font.Bold = True
                    .ForeColor = RGB(255, 0, 0)
                End With
                
                Set Obj = Form_Conso.Controls.Add("forms.checkbox.1")
                With Obj
                    .Name = "CB_" & k
                    .Left = 1110
                    .Top = 130 + (k - 1) * 14
                    .Width = 12
                    .Height = 12
                End With
                
            Next k
                        
            Set Obj = Form_Conso.Controls.Add("forms.Label.1")
            With Obj
                .Name = "Calcul"
                .Object.Caption = 0
                .Left = 1014
                .Top = 124 + (k) * 14
                .Width = 90
                .Height = 18
                .BackColor = Back
                .Font.Name = "Tahoma"
                .Font.Size = 14
                .Font.Bold = True
                .ForeColor = RGB(255, 0, 0)
            End With
            
            Form_Conso.Show

A+
 

patricktoulon

XLDnaute Barbatruc
bonjour
a ouais t'es gentil toi
il en manque la moitié
le i+k comment je fait pour deviner ce que ca vaut "i" moi
et ".object.caption " !!!;; on est dans une feuille ou un userform?
et les valeurs de la colonne 19 je les ai pas moi
et puis pourquoi tu construit tes controls dynamiques avant le show ça a son importance ça aussi !!!
bref il faudrait faire plus d’effort dans vos demandes ,il n'y a pas de devin dans ce forum
;)
 

patricktoulon

XLDnaute Barbatruc
bon j'ai bricolé quelque chose
il te faut taguer tes label avec le nom du checkbox au quel il correspond et cela parce que tu construit avant le show
si tu construisais dans le activate du userform ca serait plus simple

donc voici ta sub de création + show
VB:
Sub test()
 Dim i&
              i = 1
              For k = 1 To 3
                Set Obj = Form_Conso.Controls.Add("forms.Label.1")
                With Obj
                    .Name = "Stock" & k
                    .Caption = Cells(i + k - 1, 19)
                    .Left = 1014
                    .Top = 130 + (k - 1) * 14
                    .Width = 90
                    .Height = 12
                    .Tag = "CB_" & k
                    '.BackColor = Back
                    .Font.Name = "Tahoma"
                    .Font.Size = 10
                    .Font.Bold = True
                    .ForeColor = RGB(255, 0, 0)
                End With
                
                Set Obj = Form_Conso.Controls.Add("forms.checkbox.1")
                With Obj
                    .Name = "CB_" & k
                    .Left = 1110
                    .Top = 130 + (k - 1) * 14
                    .Tag = "check"
                    .Width = 12
                    .Height = 12
                End With
                
            Next k
                        
            Set Obj = Form_Conso.Controls.Add("forms.Label.1")
            With Obj
                .Name = "Calcul"
                .Caption = 0
                .Left = 1014
                .Top = 124 + (k) * 14
                .Width = 90
                .Height = 18
                .BackColor = Back
                .Font.Name = "Tahoma"
                .ForeColor = vbRed
                .Font.Size = 14
                .Font.Bold = True
                .ForeColor = RGB(255, 0, 0)
            End With
          Form_Conso.Show
End Sub

et voici ce que tu dois placer dans ton userform
VB:
Public WithEvents checkboxs As msforms.checkbox
Dim cls() As New Form_Conso
Public comptelabel As Long

Private Sub CheckBoxs_Change()
    Dim compte&
    Form_Conso.Controls("Calcul").Caption = 0
    For Each ctrl In Form_Conso.Controls
        If TypeName(ctrl) = "Label" Then
            If ctrl.Tag <> "" Then
                If Form_Conso.Controls(ctrl.Tag).Value = True Then
                    Form_Conso.Controls("Calcul").Caption = Val(Form_Conso.Controls("Calcul").Caption) + Val(ctrl.Caption)
                End If
            End If
        End If
    Next
  End Sub

Private Sub UserForm_Activate()
    For Each ctrl In Me.Controls
        If Left(ctrl.Tag, 5) = "check" Then
            a = a + 1: ReDim Preserve cls(1 To a): Set cls(a).checkboxs = ctrl
        End If
    Next
End Sub
resultat
demo7.gif


a l'avenir fait des efforts sur la présentation de ton problème ;)
 

Discussions similaires

Statistiques des forums

Discussions
312 236
Messages
2 086 479
Membres
103 232
dernier inscrit
logan035