Comment remplacer par For i=1 to 15

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
 

Papou-net

XLDnaute Barbatruc
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.
 

JCGL

XLDnaute Barbatruc
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
 

Cougar

XLDnaute Impliqué
Re : Comment remplacer par For i=1 to 15

Bonjour Papou-net, JCGL,

Je joins mon fichier car j'ai l'erreur "Objet requis".

Merci pour votre aide.
 

Pièces jointes

  • test Cougar.xlsm
    29.5 KB · Affichages: 36
  • test Cougar.xlsm
    29.5 KB · Affichages: 40
  • test Cougar.xlsm
    29.5 KB · Affichages: 40
Dernière édition:

JCGL

XLDnaute Barbatruc
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
 

Dranreb

XLDnaute Barbatruc
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:

Papou-net

XLDnaute Barbatruc
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.
 

Dranreb

XLDnaute Barbatruc
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:

Discussions similaires

Réponses
11
Affichages
304
Réponses
4
Affichages
218

Statistiques des forums

Discussions
312 348
Messages
2 087 510
Membres
103 570
dernier inscrit
patrickb83p