Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.

Boucle sur listbox (Résolu)

  • Initiateur de la discussion Initiateur de la discussion eideal44
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

E

eideal44

Guest
Bonsoir,
J'aurais besoin d'un coup de main car j'ai créé du code sur excel mais je souhaiterais l'allégé si c'était possible.
Je vous mets mon code VBA :

Sub SEMAINE_ENTREE()
SEMAINE = DatePart("ww", TextBox4.Value, 2, 2)
If ListBox2.ListIndex = 0 Then
Cells(7, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(7, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf ListBox2.ListIndex = 1 Then
Cells(23, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(23, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf ListBox2.ListIndex = 2 Then
Cells(39, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(39, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf ListBox2.ListIndex = 3 Then
Cells(55, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(55, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf ListBox2.ListIndex = 4 Then
Cells(71, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(71, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf ListBox2.ListIndex = 5 Then
Cells(87, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(87, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf ListBox2.ListIndex = 6 Then
Cells(103, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(103, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf ListBox2.ListIndex = 7 Then
Cells(119, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(119, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf ListBox2.ListIndex = 8 Then
Cells(135, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(135, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf ListBox2.ListIndex = 9 Then
Cells(151, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(151, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf ListBox2.ListIndex = 10 Then
Cells(167, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(167, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf ListBox2.ListIndex = 11 Then
Cells(183, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(183, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
End If
End Sub

Si quelqu'un pouvait m'aider, ça serait génial car je commence à devenir mou du cerveau.

D'avance merci.

Eideal
 
Dernière modification par un modérateur:
Re : Boucle sur listbox

Bonjour,

Essaie comme ceci :

VB:
Sub SEMAINE_ENTREE()
Dim X As Integer, Ladate As Date, Semaine As Integer

If IsDate(TextBox4.Value) Then
    Ladate = CDate(TextBox4.Value)
Else
    MsgBox "La date saisie n'est pas dans un format date reconnu par Excel."
    Exit Sub
End If
Semaine = DatePart("ww", Ladate, 2, 2)
 
X = ListBox2.ListIndex
For a = 7 To 183 Step 16
    Select Case X
        Case 0
            Cells(a, 4 + Semaine).Interior.ColorIndex = 10
            Cells(a, 4 + Semaine).Value = Left(TextBox4.Value, 2)
    End Select
Next
End Sub
 
Re : Boucle sur listbox

ça fontionne très bien sauf que ça me met les infos sur toutes les lignes (ligne 7, ligne 23, ligne 39,...), ce qui est logique avec la boucle. Est ce qu'on a la possibilité de déterminer une seule ligne où copier les informations, c'est à dire : si je choisis JANVIER dans listbox2 (listindex = 0), ça recopie l'information que sur la ligne 7, si je choisis FEVRIER dans le listbox (listindex = 1), ça recopie l'information que sur la ligne 23, ... et ainsi de suite jusqu'à DECEMBLE (listindex = 11). Est-ce que c'est possible??? D'avance merci
Eideal44
 
Re : Boucle sur listbox

Et ceci :

VB:
Sub SEMAINE_ENTREE()
Dim X As Integer, Ladate As Date
Dim Semaine As Integer, B As Integer
 
If IsDate(TextBox4.Value) Then
    Ladate = CDate(TextBox4.Value)
Else
    MsgBox "La date saisie n'est pas dans un format date reconnu par Excel."
    Exit Sub
End If
Semaine = DatePart("ww", Ladate, 2, 2)
If listbox2.ListIndex <> -1 Then
    X = listbox2.ListIndex
    For a = 7 To 183 Step 16
        Select Case X
            Case B
                Cells(a, 4 + Semaine).Interior.ColorIndex = 10
                Cells(a, 4 + Semaine).Value = Left(TextBox4.Value, 2)
                Exit For
        End Select
    Next
End Sub
 
Re : Boucle sur listbox

j'ai trouvé une solution sans boucle :

SEMAINE = DatePart("ww", TextBox4.Value, 2, 2)
Mois = DatePart("m", TextBox4.Value, 2, 2)

B = 7

If Mois = 1 Then
Cells(B, 4 + SEMAINE).Interior.ColorIndex = 10
Cells(B, 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
ElseIf Mois > 1 Then
Cells(B + (16 * (Mois - 1)), 4 + SEMAINE).Interior.ColorIndex = 10
Cells(B + (16 * (Mois - 1)), 4 + SEMAINE).Value = Left(TextBox4.Value, 2)
End If

ça fonctionne

Merci MichD
 
Re : Boucle sur listbox

le CASE B correspond à quoi?

Dans la boucle lorsque B sera égale à listindex, écriture dans la feuille.


Dernière tentative :

VB:
Sub SEMAINE_ENTREE()
 Dim X As Integer, Ladate As Date
 Dim Semaine As Integer, B As Integer
  
 If IsDate(TextBox4.Value) Then
     Ladate = CDate(TextBox4.Value)
 Else
     MsgBox "La date saisie n'est pas dans un format date reconnu par Excel."
     Exit Sub
 End If
 Semaine = DatePart("ww", Ladate, 2, 2)
 If ListBox2.ListIndex <> -1 Then
     X = ListBox2.ListIndex
     For a = 7 To 183 Step 16
         Select Case X = B
             Case True
                 Cells(a, 4 + Semaine).Interior.ColorIndex = 10
                 Cells(a, 4 + Semaine).Value = Left(TextBox4.Value, 2)
                 Exit For
         End Select
         B = B + 1
     Next
 End Sub
 
Dernière édition:
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD
Assurez vous de marquer un message comme solution pour une meilleure transparence.

Discussions similaires

Réponses
10
Affichages
645
M
Réponses
3
Affichages
2 K
MONADESIGN82
M
Réponses
8
Affichages
1 K
S
Réponses
4
Affichages
1 K
sgangbadjo
S
P
Réponses
7
Affichages
1 K
plaiiz
P
B
Réponses
2
Affichages
2 K
bastienb
B
F
Réponses
17
Affichages
2 K
Fariri
F
Réponses
8
Affichages
2 K
C
  • Résolu(e)
XL 2019 CHECKBOX
Réponses
3
Affichages
1 K
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…