Amélioration/simplification de mon code

Résolu/Fermé
bassmart Messages postés 281 Date d'inscription jeudi 19 février 2015 Statut Membre Dernière intervention 19 décembre 2023 - 10 mai 2016 à 22:11
Whismeril Messages postés 19027 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 24 avril 2024 - 11 mai 2016 à 16:23
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!


A voir également:

2 réponses

Whismeril Messages postés 19027 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 24 avril 2024 931
10 mai 2016 à 22:52
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
bassmart Messages postés 281 Date d'inscription jeudi 19 février 2015 Statut Membre Dernière intervention 19 décembre 2023 1
11 mai 2016 à 13:49
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
f894009 Messages postés 17185 Date d'inscription dimanche 25 novembre 2007 Statut Membre Dernière intervention 15 avril 2024 1 701
11 mai 2016 à 14:14
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
bassmart Messages postés 281 Date d'inscription jeudi 19 février 2015 Statut Membre Dernière intervention 19 décembre 2023 1
11 mai 2016 à 14:30
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
Whismeril Messages postés 19027 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 24 avril 2024 931
11 mai 2016 à 16:23
Bonsoir,
copier / coller c'est le secret!
0