Amélioration/simplification de mon code

Résolu
bassmart Messages postés 281 Date d'inscription   Statut Membre Dernière intervention   -  
 Utilisateur anonyme -
Bonjour,

J'aimerais savoir comment puis-je améliorer/simplifier le code suivant.
If ComboBox1 <= 3 Then
    Sheets.Add.Move after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Graphique"
    Call zone_texte
    Call graphique
ElseIf ComboBox1 <= 6 Then
    Sheets.Add.Move after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Graphique"
    Call zone_texte
    Sheets.Add.Move after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Graphique2"
    Call zone_texte
    Call graphique
ElseIf ComboBox1 <= 9 Then
    Sheets.Add.Move after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Graphique"
    Call zone_texte
    Sheets.Add.Move after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Graphique2"
    Call zone_texte
    Sheets.Add.Move after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Graphique3"
    Call zone_texte
    Call graphique
End If


Ce code me sert à ajouter des feuilles à une classeur en fonction du numéro inscrit dans mon ComboBox1 pour faire afficher ensuite des graphiques. La seul façon d'y arriver qui fonctionne jusqu'à maintenant, c'est de répèter mes premières lignes de codes.

Merci!

2 réponses

  1. Utilisateur anonyme
     
    Bonsoir,

    En mettant ce code dans un sub ou Fuction qui ne sert qu'à ça:

    Sub Tito
        Sheets.Add.Move after:=Sheets(Sheets.Count)
        ActiveSheet.Name = "Graphique"
        Call zone_texte
    
    If ComboBox1 <= 3 Then
         Call graphique
         Exit Sub
    End if
    
        Sheets.Add.Move after:=Sheets(Sheets.Count)
        ActiveSheet.Name = "Graphique2"
        Call zone_texte
    
    If ComboBox1 <= 6 Then
         Call graphique
         Exit Sub
    End if
    
        Sheets.Add.Move after:=Sheets(Sheets.Count)
        ActiveSheet.Name = "Graphique3"
        Call zone_texte
        Call graphique
    End sub
    

    0
    1. bassmart Messages postés 281 Date d'inscription   Statut Membre Dernière intervention   1
       
      Merci pour la réponse, mais ce n'est pas tout à fait ça!

      Si ComboBox1 est inférieur ou égal à 3, il ajoute uniquement la feuille "graphique", si Combobox1 est inférieur ou égal à 6, il ajoute la feuille "graphique" et "graphique2" et si Combobox1 est inférieur ou égal à 9, il ajoute les trois feuilles.

      merci!
      0
  2. f894009 Messages postés 17417 Date d'inscription   Statut Membre Dernière intervention   1 717
     
    Bonjour a vous deux,

    ceci devrait aller

    Private Sub ComboBox1_Change()
        If ComboBox1 <> "" Then
            Tito (ComboBox1.Value)
        End If
    End Sub


    Sub Tito(Choix)
        Sheets.Add.Move after:=Sheets(Sheets.Count)
        ActiveSheet.Name = "Graphique"
        Call zone_texte
    
        If Choix <= 3 Then
            Call graphique
            Exit Sub
        End If
    
        Sheets.Add.Move after:=Sheets(Sheets.Count)
        ActiveSheet.Name = "Graphique2"
        Call zone_texte
    
        If Choix <= 6 Then
            Call graphique
            Exit Sub
        End If
    
        If Choix <= 9 Then
            Call graphique
            Sheets.Add.Move after:=Sheets(Sheets.Count)
            ActiveSheet.Name = "Graphique3"
            Call zone_texte
            Call graphique
        End If
    End Sub


    Mais vous n'avez pas droit a l'erreur, vous n'avez qu'un seul choix!!!!!!!!!
    0
    1. bassmart Messages postés 281 Date d'inscription   Statut Membre Dernière intervention   1
       
      Merci beaucoup!

      Finalement les 2 codes marches, en recopiant le code je n'avais tout simplement pas mis les exit sub dans mon code.

      Ça m'apprendra, la prochaine fois je vais tout simplement copier le code!

      Merci à vous deux!
      0
    2. Utilisateur anonyme
       
      Bonsoir,
      copier / coller c'est le secret!
      0