Comment remplacer par For i=1 to 15

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 !

Cougar

XLDnaute Impliqué
Bonjour,

Voici un exemple que je dois répéter 15 fois. La seule différence entre ces ligne est mon # de combobox et de Textbox.

Comment écrire le : For i= 1 to 15 ?

If ComboBox1.ListIndex = -1 Then GoTo Données
Cells(ComboBox1.ListIndex + 2, 3) = Cells(ComboBox1.ListIndex + 2, 2) * Val(TextBox31)

If ComboBox2.ListIndex = -1 Then GoTo Données
Cells(ComboBox2.ListIndex + 2, 3) = Cells(ComboBox2.ListIndex + 2, 2) * Val(TextBox32)

If ComboBox3.ListIndex = -1 Then GoTo Données
Cells(ComboBox3.ListIndex + 2, 3) = Cells(ComboBox3.ListIndex + 2, 2) * Val(TextBox33)

Merci
 
Re : Comment remplacer par For i=1 to 15

Bonsoir Cougar,

Essaie comme ceci (pas testé):

Code:
For i = 1 To 5
  If ComboBox1.ListIndex = -1 Then GoTo Données
  Cells(ComboBox1.ListIndex + 2, 3) = Cells(ComboBox1.ListIndex + 2, 2) * Val(Me.Controls("TextBox" & 30 + i)
Next i
Cordialement.
 
Re : Comment remplacer par For i=1 to 15

Bonjour à tous,

Evidemment pas testé sur le fichier non joint à la présente demande :

VB:
For i = 1 To 15
If ComboBox & i.ListIndex = -1 Then GoTo Données
Cells(ComboBox & i.ListIndex + 2, 3) = Cells(ComboBox & i.ListIndex + 2, 2) * Val(TextBox & 30 + i)
Next i

A+ à tous
 
Re : Comment remplacer par For i=1 to 15

Bonjour à tous,

Peux-tu essayer :

VB:
Private Sub CommandButton1_Click()
    With Sheets("Chèvre")
        For i = 1 To 15
            If (Controls("Combobox" & i).ListIndex) = -1 Then GoTo Données
            Cells(Controls("Combobox" & i).ListIndex + 2, 3) = Cells(Controls("Combobox" & i).ListIndex + 2, 2) * Controls("TextBox" & 30 + i).Value
        Next i
    End With
Données:
    Columns("C:C").Select
    Selection.NumberFormat = "General"
    Unload Me
End Sub

A+ à tous
 
Re : Comment remplacer par For i=1 to 15

Bonsoir.

Essayez comme ça :
VB:
Private Sub CommandButton1_Click()
Dim F As Worksheet, I As Long, Cbx As MSForms.ComboBox, L As Long
Set F = Worksheets("Chèvre")
For I = 1 To 15
   Set Cbx = Me("ComboBox" & I)
   L = Cbx.ListIndex + 2
   If L < 2 Then Exit For
   F.Cells(L, 3).Value = F.Cells(L, 2) * Val(Me("TextBox" & 30 + I).Text)
   Next I
Columns("C").NumberFormat = "General"
Unload Me
End Sub
Car avec la solution de JCGL (que je salue), il va prendre des cellules de la feuille active, qui n'est pas forcément Chèvre.
 
Dernière édition:
Re : Comment remplacer par For i=1 to 15

RE:

Ceci devrait fonctionner:

Code:
Private Sub CommandButton1_Click()
With Sheets("Chèvre")
    For i = 1 To 15
        If (Me.Controls("ComboBox" & i).ListIndex) = -1 Then GoTo Données
        Cells(Me.Controls("ComboBox" & i).ListIndex + 2, 3) = Cells(Me.Controls("ComboBox" & i).ListIndex + 2, 2) * Val(Me.Controls("TextBox" & 30 + i))
    Next i
End With
Données:
    Columns("C:C").Select
    Selection.NumberFormat = "General"
    Unload Me
End Sub
Cordialement.

PS: Salut JCGL, Dranreb.
 
Re : Comment remplacer par For i=1 to 15

Pareil: .Cells(… J'avais d'ailleurs moi même oublié un F.Cells(…, ce que je viens de corriger.

Edit, je répondais au poste précédent de Papou-net, que je salue aussi.
 
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

Discussions similaires

Réponses
10
Affichages
633
Réponses
3
Affichages
310
Réponses
5
Affichages
839
Réponses
15
Affichages
656
Réponses
8
Affichages
749
  • Question Question
XL 2021 VBA excel
Réponses
4
Affichages
432
Réponses
3
Affichages
903
Réponses
5
Affichages
551
Retour