Liste déroulante des onglets dans un userForm

Résolu
mouftie -  
 mouftie -
Bonjour,
J'ai créer un userform menu, en VBA, qui permet :
de choisir une Date qui met à jour un modèle de saisie du mois choisit. Chaque nouveau mois crée un nouvel onglet

Je voudrais ajouter à mon UserForm une liste déroulante des onglets présents et donc que le choix dans la liste ouvre l'onglet correspondant.

Quelqu'un peut-il m'aider ? SVP
Merci

2 réponses

  1. Patrice33740 Messages postés 8400 Date d'inscription   Statut Membre Dernière intervention   1 783
     
    Bonjour,

    Code :
    Option Explicit
    
    Private Sub UserForm_Initialize()
      ....
      ....
      Call ComboBox1_Initialize
    End Sub
    
    Private Sub ComboBox1_Change()
      ThisWorkbook.Worksheets(ComboBox1.Value).Activate
    End Sub
    
    Private Sub ComboBox1_Initialize()
    Dim wsh As Worksheet
      ComboBox1.Clear
      For Each wsh In ThisWorkbook.Worksheets
        ComboBox1.AddItem wsh.Name
      Next wsh
    End Sub


    Ajoute à la fin de chaque procédure qui modifie le nombre d'onglets :
    Call ComboBox1_Initialize 
    0
  2. mouftie
     
    Bonjour Patrice,
    Merci pour ta réponse, ce code permet effectivement d'avoir un menu déroulant des onglets existants.
    Cependant la combobox ne se réinitialise pas si on ajoute ou supprime un conglet
    0
    1. Patrice33740 Messages postés 8400 Date d'inscription   Statut Membre Dernière intervention   1 783
       
      Comme je t'ai dis, il faut ajouter :
      Call ComboBox1_Initialize 
      à la fin de chaque procédure qui modifie le nombre d'onglets.
      0
    2. Patrice33740 Messages postés 8400 Date d'inscription   Statut Membre Dernière intervention   1 783
       
      Voici un code qui fonctionne aussi avec un formulaire non modal :

      Dans le module UserForm1 :
      Option Explicit  
      Dim SansEvénements As Boolean  
      
      Private Sub UserForm_Initialize()  
        Application.EnableEvents = True  
        Call ComboBox1_Initialize  
      End Sub  
      
      Private Sub ComboBox1_Change()  
        If SansEvénements Then Exit Sub  
        ThisWorkbook.Worksheets(ComboBox1.Value).Activate  
      End Sub  
      
      Public Sub ComboBox1_Initialize()  
      Dim wsh As Worksheet  
        SansEvénements = True  
        ComboBox1.Clear  
        For Each wsh In ThisWorkbook.Worksheets  
          ComboBox1.AddItem wsh.Name  
        Next wsh  
        ComboBox1.Value = ActiveSheet.Name  
        SansEvénements = False  
      End Sub

      Dans le module ThisWorkbook :
      Private Sub Workbook_SheetActivate(ByVal Sh As Object)  
        Call UserForm1.ComboBox1_Initialize  
      End Sub

      Pas besoin d'ajouter d'autre appel à l'initialisation du combobox1

      Patrice
      0
    3. mouftie
       
      Résolu - Merci
      0