Elseif + calucul sur i racourcir le code?

Bonjour, bonsoir à tous,

je cale sur un problème, je doit faire un calcul via des textbox :

    TextBox125.Value = CDbl(TextBox1.Value) + CDbl(TextBox3.Value) + CDbl(TextBox37.Value) + CDbl(TextBox39.Value) 

mais sous certaines conditions, la textbox1 et textbox3 peuvent changer... j ai donc fait une contion if...

For i = 197 To 199
If Me.Controls("TextBox" & i) = "A" Then

    If i = 197 Then
       TextBox125.Value = CDbl(TextBox25.Value) + CDbl(TextBox27.Value) + CDbl(TextBox37.Value) + CDbl(TextBox39.Value) 
    ElseIf i = 198 Then
        TextBox125.Value = CDbl(TextBox29.Value) + CDbl(TextBox31.Value) + CDbl(TextBox37.Value) + CDbl(TextBox39.Value) 
    ElseIf i = 199 Then
         TextBox125.Value = CDbl(TextBox33.Value) + CDbl(TextBox35.Value) + CDbl(TextBox37.Value) + CDbl(TextBox39.Value) 
    Else
       TextBox125.Value = CDbl(TextBox1.Value) + CDbl(TextBox3.Value) + CDbl(TextBox37.Value) + CDbl(TextBox39.Value)
    End If
    End If
Next i

il faudrait bien entendu ajouter à cela :

For i = 197 To 199
If Me.Controls("TextBox" & i) = "B" Then

    If i = 197 Then
       TextBox125.Value = CDbl(TextBox25.Value) + CDbl(TextBox27.Value) + CDbl(TextBox41.Value) + CDbl(TextBox43.Value) 
    ElseIf i = 198 Then
        TextBox125.Value = CDbl(TextBox29.Value) + CDbl(TextBox31.Value) + CDbl(TextBox41.Value) + CDbl(TextBox43.Value) 
    ElseIf i = 199 Then
         TextBox125.Value = CDbl(TextBox33.Value) + CDbl(TextBox35.Value) + CDbl(TextBox41.Value) + CDbl(TextBox43.Value) 
    Else
       TextBox125.Value = CDbl(TextBox1.Value) + CDbl(TextBox3.Value) + CDbl(TextBox41.Value) + CDbl(TextBox43.Value)
    End If
    End If
Next i

et ce jusque E... ce qui me ferais un code assez long...

au final je suis arrivé a ce resultat

Sub changement2()

If TextBox197 = "A" Then
    TextBox125.Value = CDbl(TextBox25.Value) + CDbl(TextBox27.Value) + CDbl(TextBox37.Value) + CDbl(TextBox39.Value) + CDbl(TextBox73.Value) + CDbl(TextBox75.Value)
ElseIf TextBox197 = "B" Then
    TextBox129.Value = CDbl(TextBox25.Value) + CDbl(TextBox27.Value) + CDbl(TextBox41.Value) + CDbl(TextBox43.Value) + CDbl(TextBox77.Value) + CDbl(TextBox79.Value)
ElseIf TextBox197 = "C" Then
    TextBox133.Value = CDbl(TextBox25.Value) + CDbl(TextBox27.Value) + CDbl(TextBox45.Value) + CDbl(TextBox47.Value) + CDbl(TextBox81.Value) + CDbl(TextBox83.Value)
ElseIf TextBox197 = "D" Then
    TextBox137.Value = CDbl(TextBox25.Value) + CDbl(TextBox27.Value) + CDbl(TextBox49.Value) + CDbl(TextBox51.Value) + CDbl(TextBox85.Value) + CDbl(TextBox87.Value)
ElseIf TextBox197 = "E" Then
    TextBox141.Value = CDbl(TextBox25.Value) + CDbl(TextBox27.Value) + CDbl(TextBox53.Value) + CDbl(TextBox55.Value) + CDbl(TextBox89.Value) + CDbl(TextBox91.Value)
ElseIf TextBox197 = "F" Then
    TextBox145.Value = CDbl(TextBox25.Value) + CDbl(TextBox27.Value) + CDbl(TextBox57.Value) + CDbl(TextBox59.Value) + CDbl(TextBox93.Value) + CDbl(TextBox95.Value)
ElseIf TextBox198 = "A" Then
    TextBox129.Value = CDbl(TextBox29.Value) + CDbl(TextBox31.Value) + CDbl(TextBox41.Value) + CDbl(TextBox43.Value) + CDbl(TextBox77.Value) + CDbl(TextBox79.Value)
ElseIf TextBox198 = "B" Then
    TextBox133.Value = CDbl(TextBox29.Value) + CDbl(TextBox31.Value) + CDbl(TextBox45.Value) + CDbl(TextBox47.Value) + CDbl(TextBox81.Value) + CDbl(TextBox83.Value)
ElseIf TextBox198 = "C" Then
    TextBox137.Value = CDbl(TextBox29.Value) + CDbl(TextBox31.Value) + CDbl(TextBox49.Value) + CDbl(TextBox51.Value) + CDbl(TextBox85.Value) + CDbl(TextBox87.Value)
ElseIf TextBox198 = "D" Then
    TextBox141.Value = CDbl(TextBox29.Value) + CDbl(TextBox31.Value) + CDbl(TextBox53.Value) + CDbl(TextBox55.Value) + CDbl(TextBox89.Value) + CDbl(TextBox91.Value)
ElseIf TextBox198 = "E" Then
    TextBox145.Value = CDbl(TextBox29.Value) + CDbl(TextBox31.Value) + CDbl(TextBox57.Value) + CDbl(TextBox59.Value) + CDbl(TextBox93.Value) + CDbl(TextBox95.Value)
ElseIf TextBox198 = "F" Then
    TextBox145.Value = CDbl(TextBox29.Value) + CDbl(TextBox31.Value) + CDbl(TextBox57.Value) + CDbl(TextBox59.Value) + CDbl(TextBox93.Value) + CDbl(TextBox95.Value)
ElseIf TextBox199 = "A" Then
    TextBox129.Value = CDbl(TextBox33.Value) + CDbl(TextBox34.Value) + CDbl(TextBox41.Value) + CDbl(TextBox43.Value) + CDbl(TextBox77.Value) + CDbl(TextBox79.Value)
ElseIf TextBox199 = "B" Then
    TextBox133.Value = CDbl(TextBox33.Value) + CDbl(TextBox34.Value) + CDbl(TextBox45.Value) + CDbl(TextBox47.Value) + CDbl(TextBox81.Value) + CDbl(TextBox83.Value)
ElseIf TextBox199 = "C" Then
    TextBox137.Value = CDbl(TextBox33.Value) + CDbl(TextBox34.Value) + CDbl(TextBox49.Value) + CDbl(TextBox51.Value) + CDbl(TextBox85.Value) + CDbl(TextBox87.Value)
ElseIf TextBox199 = "D" Then
    TextBox141.Value = CDbl(TextBox33.Value) + CDbl(TextBox34.Value) + CDbl(TextBox53.Value) + CDbl(TextBox55.Value) + CDbl(TextBox89.Value) + CDbl(TextBox91.Value)
ElseIf TextBox199 = "E" Then
    TextBox145.Value = CDbl(TextBox33.Value) + CDbl(TextBox34.Value) + CDbl(TextBox57.Value) + CDbl(TextBox59.Value) + CDbl(TextBox93.Value) + CDbl(TextBox95.Value)
ElseIf TextBox199 = "F" Then
    TextBox145.Value = CDbl(TextBox33.Value) + CDbl(TextBox34.Value) + CDbl(TextBox57.Value) + CDbl(TextBox59.Value) + CDbl(TextBox93.Value) + CDbl(TextBox95.Value)
Else
    TextBox125.Value = CDbl(TextBox1.Value) + CDbl(TextBox3.Value) + CDbl(TextBox37.Value) + CDbl(TextBox39.Value) + CDbl(TextBox73.Value) + CDbl(TextBox75.Value)
    TextBox129.Value = CDbl(TextBox5.Value) + CDbl(TextBox7.Value) + CDbl(TextBox41.Value) + CDbl(TextBox43.Value) + CDbl(TextBox77.Value) + CDbl(TextBox79.Value)
    TextBox133.Value = CDbl(TextBox9.Value) + CDbl(TextBox11.Value) + CDbl(TextBox45.Value) + CDbl(TextBox47.Value) + CDbl(TextBox81.Value) + CDbl(TextBox83.Value)
    TextBox137.Value = CDbl(TextBox13.Value) + CDbl(TextBox15.Value) + CDbl(TextBox49.Value) + CDbl(TextBox51.Value) + CDbl(TextBox85.Value) + CDbl(TextBox87.Value)
    TextBox141.Value = CDbl(TextBox17.Value) + CDbl(TextBox19.Value) + CDbl(TextBox53.Value) + CDbl(TextBox55.Value) + CDbl(TextBox89.Value) + CDbl(TextBox91.Value)
    TextBox145.Value = CDbl(TextBox21.Value) + CDbl(TextBox23.Value) + CDbl(TextBox57.Value) + CDbl(TextBox59.Value) + CDbl(TextBox93.Value) + CDbl(TextBox95.Value)
    TextBox149.Value = CDbl(TextBox25.Value) + CDbl(TextBox27.Value) + CDbl(TextBox61.Value) + CDbl(TextBox63.Value) + CDbl(TextBox97.Value) + CDbl(TextBox99.Value)
    TextBox153.Value = CDbl(TextBox29.Value) + CDbl(TextBox31.Value) + CDbl(TextBox65.Value) + CDbl(TextBox67.Value) + CDbl(TextBox101.Value) + CDbl(TextBox103.Value)
    TextBox157.Value = CDbl(TextBox33.Value) + CDbl(TextBox35.Value) + CDbl(TextBox69.Value) + CDbl(TextBox71.Value) + CDbl(TextBox105.Value) + CDbl(TextBox107.Value)
End Sub

qui evidement fait ce que je lui demande mais qui me semble fort fort long, quelq un aurait une idée afin de faire plus simple?

merci beaucoup

Bonsoir,

Tu peux essayer quelque chose comme ça :

    Dim Ct(1 To 4) As Control, a%
    '[...]
    For i = 197 To 199
        a = 25 + (i - 197) * 4
        Set Ct(1) = Me.Controls("TextBox" & a)
        Set Ct(2) = Me.Controls("TextBox" & a + 2)
        a = 37 + (Asc(Me.Controls("TextBox" & i).Value) - 65) * 4
        Set Ct(3) = Me.Controls("TextBox" & a)
        Set Ct(4) = Me.Controls("TextBox" & a + 2)
        TextBox125.Value = CDbl(Ct(1)) + CDbl(Ct(2)) + CDbl(Ct(3)) + CDbl(Ct(4))
    Next i

Petite anomalie dans ton code initial : dans la boucle i de 197 à 199, i ne pourra prendre d'autre valeur que 197, 198 ou 199, donc ton cas Else impliquant les TextBox1 et 3 ne peut se produire, et n'est donc pas pris en compte dans le code ci-dessus.

Cordialement.

bonjour,

merci pour ta reponse, j ai esayer de travailler sur ce que tu m'a donnée, en essayant de comprendre par moi meme

voila ce que ca a donnée

Sub changement()

For i = 197 To 199
If Me.Controls("TextBox" & i) = "" Then
a = 1
ch1 = Me.Controls("TextBox" & a)
ch2 = Me.Controls("TextBox" & a + 2)
ch3 = Me.Controls("TextBox" & a + 36)
ch4 = Me.Controls("TextBox" & a + 38)
ch5 = Me.Controls("TextBox" & a + 72)
ch6 = Me.Controls("TextBox" & a + 74)
TextBox125.Value = CDbl(ch1) + CDbl(ch2) + CDbl(ch3) + CDbl(ch4) + CDbl(ch5) + CDbl(ch6)
Else
If Me.Controls("TextBox" & i) = "A" Then

    If i = 197 Then
    a = 25
    ElseIf i = 198 Then
    a = 29
    ElseIf i = 199 Then
    a = 33
End If
TextBox125.Value = CDbl(ch1) + CDbl(ch2) + CDbl(ch3) + CDbl(ch4) + CDbl(ch5) + CDbl(ch6)
End If
End If
Next i

End Sub

car évidement j'en aurais besoin pour d autre calcul, je n'allais pas tous les mettre

ic tout fonctionne ou presque mdr, car quand TextBox" & i) = "" pas de probléme ca me met lle calcul mais il ne veux plus le faire si TextBox" & i) = "A" il me met 0 dans la textbox125

j ai essayer de le retourné dans tous les sens et je sais pas pourquoi il le prend pas... a tu une idee?

On calcule avec des nombres...

Tu noteras que j'avais utilisé les codes de caractères (des nombres !) pour tes lettres A à E.

Après, j'avais utilisé des variables objets pour définir les TextBox utilisés dans l'addition, tu passes à des valeurs et de 4 précédemment à 6...

Je n'ai guère le temps de voir à chaque modification ce que cela recouvre ! Tu as une méthode générale, tu l'adaptes selon tes modifications.

Cordialement.

Rechercher des sujets similaires à "elseif calucul racourcir code"