Accéder à un objet crée sous condition ds un userform

Help me please -  
cs_Le Pivert Messages postés 8437 Statut Contributeur -
Bonjour

J'ai créé un UserForm dans lequel se place des OptionButton en fonction du remplissage d'une colonne dans Excel. J'arrive bien à afficher cet userform, mais j'aimerais bien effectuer un test pour savoir si une case à été cochée. Voilà donc mon code:
Dim i As Integer
For i = 1 To 7 '7=nbre optionButton
If Me.Controls.["OptionButton"&i].Value=True Then
[...]
End If
Next

Et lors du passage sur le test, il me mets "Propriété ou méthode non gérée par cet objet".
Donc si quelqu'un à la réponse, merci de m'aider.

2 réponses

  1. cs_Le Pivert Messages postés 8437 Statut Contributeur 730
     
    Bonjour,

    Voir ceci:

    https://silkyroad.developpez.com/VBA/ControlesUserForm/

    Dim Ctrl As Control
        For Each Ctrl In UserForm1.Controls
       If Ctrl.Value = True Then
        MsgBox Ctrl.Name
         End If
        Next
    0
    1. Help me please
       
      En suivant le lien, voila ce que j'ai donc écrit en bouclant sur les optionbutton:

      Dim ctrl As Control
      Dim j As Integer
      j = 1
      For Each ctrl In Me.Controls
      If TypeOf ctrl Is MSForms.OptionButton Then
      If "OptionButton" & j = True Then
      MsgBox "OptionButton" & j
      End If
      j = j + 1
      End If
      Next ctrl


      Et j'ai un message d'erreur "Incompatibilité de type" sur le If "OptionButton" & j = True

      Merci de votre aide
      0
  2. cs_Le Pivert Messages postés 8437 Statut Contributeur 730
     
    Comme ceci:

    Dim Ctrl As Control
        For Each Ctrl In UserForm1.Controls
       If TypeOf Ctrl Is MSForms.OptionButton Then
       If Ctrl.Value = True Then
        MsgBox Ctrl.Name
        End If
         End If
        Next


    0