Autres VBA Nom d'une variable qui change dans une boucle For

UseJ

XLDnaute Nouveau
Bonjour,

Je me permets de vous solliciter car je bloque sur un codage VBA.

J'ai créé une boucle For x = 10 to 29, et je souhaiterais que ma variable "CA_TLS_x" prenne le nom de CA_TLS_10 puis CA_TLS_11 puis CA_TLS_12 etc... que le x soit remplacé par sa valeur, afin que dans les deux boucles For détaillées ci-dessous, mes instructions calculent d'abord CA_TLS_10, puis CA_TLS_11, .... ce que je ne parviens pas à faire.

Sub()

Dim CA_TLS_10, CA_TLS_11, CA_TLS_12, CA_TLS_13, CA_TLS_14, CA_TLS_15, CA_TLS_16, CA_TLS_17, CA_TLS_18, CA_TLS_19, CA_TLS_20, CA_TLS_21, CA_TLS_22, CA_TLS_23, CA_TLS_24, CA_TLS_25, CA_TLS_26, CA_TLS_27, CA_TLS_28, CA_TLS_29 As Single
Dim ligne As Integer
Dim x As Integer
Dim CA_TLS_x As Single

Lastrow = Worksheets("Pom informations").Range("A2").End(xlDown).Row

CA_TLS_10 = 0
CA_TLS_11 = 0
CA_TLS_12 = 0
CA_TLS_13 = 0
CA_TLS_14 = 0
CA_TLS_15 = 0
CA_TLS_16 = 0
CA_TLS_17 = 0
CA_TLS_18 = 0
CA_TLS_19 = 0
CA_TLS_20 = 0
CA_TLS_21 = 0
CA_TLS_22 = 0
CA_TLS_23 = 0
CA_TLS_24 = 0
CA_TLS_25 = 0
CA_TLS_26 = 0
CA_TLS_27 = 0
CA_TLS_28 = 0
CA_TLS_29 = 0


For x = 10 To 29 'x représentant l'index des colonnes
For ligne = 2 To Lastrow
If Worksheets("Pom informations").Cells(ligne, 31) = "TLS_8101" And IsNumeric(Worksheets("Pom informations").Cells(ligne, 9).Value) = True And IsNumeric(Worksheets("Pom informations").Cells(ligne, x).Value) = True Then
CA_TLS_x = CA_TLS_x + Cells(ligne, 9) * Cells(ligne, x)
'Je souhaiterais ici que cela me calcule CA_TLS_10 grace au calcul de Cells(ligne, 9) * Cells(ligne, x = 10), puis qu'une autre variable CA_TLS_11 soit calculée par Cells(ligne, 9) * Cells(ligne, x = 11), etc...
End If

Next ligne
Next x

'Afin d'afficher ces différents calculs dans des cellules différentes
Worksheets("SalesVol").Cells(94, 8) = CA_TLS_10
Worksheets("SalesVol").Cells(95, 8) = CA_TLS_11
Worksheets("SalesVol").Cells(96, 8) = CA_TLS_12
....
End Sub

Avez-vous une idée de comment je devrais procéder pour que cela fonctionne ?
J'ai cherché sur le forum mais je n'ai pas su trouver de réponse adaptée.

Je vous remercie par avance, et vous souhaite une bonne journée !
 
Solution
Bonjour,

Si les variables sont propres à la sub :
  • Pourquoi commencer avec CA_TLS_10 ?
  • Pourquoi ne pas faire une variable Tableau ?
Ce qui pourrait se faire ainsi :
VB:
Sub Test()

Dim CA_TLS(19)
Dim ligne As Integer
Dim x As Integer

Lastrow = Worksheets("Pom informations").Range("A2").End(xlDown).Row


For x = 0 To ubounds(CA_TLS) 'x représentant l'index des colonnes
    For ligne = 2 To Lastrow
        If Worksheets("Pom informations").Cells(ligne, 31) = "TLS_8101" _
        And IsNumeric(Worksheets("Pom informations").Cells(ligne, 9).Value) = True _
        And IsNumeric(Worksheets("Pom informations").Cells(ligne, x + 10).Value) = True Then
            CA_TLS(x) = CA_TLS(x) + Cells(ligne, 9) * Cells(ligne, x + 10)...

fanch55

XLDnaute Barbatruc
Bonjour,

Si les variables sont propres à la sub :
  • Pourquoi commencer avec CA_TLS_10 ?
  • Pourquoi ne pas faire une variable Tableau ?
Ce qui pourrait se faire ainsi :
VB:
Sub Test()

Dim CA_TLS(19)
Dim ligne As Integer
Dim x As Integer

Lastrow = Worksheets("Pom informations").Range("A2").End(xlDown).Row


For x = 0 To ubounds(CA_TLS) 'x représentant l'index des colonnes
    For ligne = 2 To Lastrow
        If Worksheets("Pom informations").Cells(ligne, 31) = "TLS_8101" _
        And IsNumeric(Worksheets("Pom informations").Cells(ligne, 9).Value) = True _
        And IsNumeric(Worksheets("Pom informations").Cells(ligne, x + 10).Value) = True Then
            CA_TLS(x) = CA_TLS(x) + Cells(ligne, 9) * Cells(ligne, x + 10)
        End If
    Next ligne
    Worksheets("SalesVol").Cells(94 + x, 8) = CA_TLS(x)
Next x

End Sub

Mais je pense qu'il peut y avoir encore plus simple si vous fournissiez un vrai classeur exemple expurgé des infos confidentielles ...
 

Discussions similaires

Réponses
2
Affichages
129
Réponses
0
Affichages
83
Réponses
17
Affichages
760

Statistiques des forums

Discussions
311 733
Messages
2 082 019
Membres
101 872
dernier inscrit
Colin T