Crée une boucle sur VBA

Résolu
Bonjour,

je voudrais crée un boucle pour ce code puisque que je vais avoir 12 If sinon ou je rajoute pour chaque :
"+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",1,""Années"",2018)"

La macros:
Sub Macro1()
'
' Macro1 Macro
'
If Range("B59") = 1 Then
   
        Range("B60").Select
        ActiveCell.FormulaR1C1 = _
        "=GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",1,""Années"",2018)"
        
    End If
If Range("B59") = 2 Then
   
        Range("B60").Select
        ActiveCell.FormulaR1C1 = _
        "=GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",1,""Années"",2018)+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",<bold>2</bold>,""Années"",2018)"
    End If
End Sub


Nombre en gras augmente de 1 a chaque boucle

Merci d'avance

2 réponses

  • Votes
  • Date
  • Date décroissante
  1. Contributeur
    Bonjour
    plutôt que d'utiliser 12 IF, utilise SELECT CASE et tu reportes le nombre dans ton deuxième get pivot au besoin en créant une variable pour le nombre.
    style

    Tu as des explications sur le select case dans l'aide en ligne de VBA


    1. Bonjour,
      merci du petit conseil déjà je gagne quelques lignes.

      Mais le souci c'est que si le chiffre est 12 j'ai donc 12 fois ce code : GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",1,""Années"",2018

      Avec le numéro en gras qui va de 1 à 12 et ça je ne sais pas faire..

      Par exemple ça donnerait ça :

      "=GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",1,""Années"",2018+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",2,""Années"",2018+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",3,""Années"",2018+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",4,""Années"",2018+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",5,""Années"",2018+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",6,""Années"",2018+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",7,""Années"",2018+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",8,""Années"",2018+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",9,""Années"",2018+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",10,""Années"",2018+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",11,""Années"",2018+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",12,""Années"",2018)"
    2. @Cailloux50Bonjour à vous 2,
      tu pourrais boucler sur un for :
      Dim tmpFormule as string
      if Range("B59").value> 1 then
          tmpFormule = "=GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production 
          Date"",1,""Années"",2018
          For i = 2 to Range("B59").value
              tmpFormule = tmpFormule & "+GETPIVOTDATA(""Production plant"",R44C1,""Production 
          plant"",""1101"",""Production Date""," & i & ",""Années"",2018"
              If i = Range("B59").value then  tmpFormule =tmpFormule & ")"
          Next i
      else
         tmpFormule =  "=GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",1,""Années"",2018)"
      end if
      Range("B60").FormulaR1C1 =tmpFormule

      je n'ai pas testé mais logiquement ca doit être bon
    3. @fabien25000Merci beaucoup,

      J'ai un tout petit peu modifier le code,

      Sub Macro2()

      Dim tmpFormule As String

      If Range("B59").Value > 1 Then
      tmpFormule = "=GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",3,""Années"",2018"
      For i = 4 To Range("B59").Value

      tmpFormule = tmpFormule & "+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date""," & i & ",""Années"",2018"
      If i = Range("B59").Value Then tmpFormule = tmpFormule & ")"
      Next i
      Else
      tmpFormule = "=GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",3,""Années"",2018)"
      End If
      Range("B60").FormulaR1C1 = tmpFormule
      End Sub

      Mais juste apres le end if ça mets une erreur 1004
    4. Sub Macro2()
      
      Dim tmpFormule As String
      
      If Range("B59").Value > 1 Then
          tmpFormule = "=GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",3,""Années"",2018)"
          For i = 4 To Range("B59").Value
              tmpFormule = tmpFormule & "+GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date""," & i & ",""Années"",2018)"
      '        If i = Range("B59").Value Then tmpFormule = tmpFormule & ")"
          Next i
          Else
          tmpFormule = "=GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",3,""Années"",2018)"
      End If
      Range("B60").FormulaR1C1 = tmpFormule
      End Sub
      

      il manquait une parenthèse désolé


    5. tjrs pareil je comprend pas parce que tous parait bon..
  2. Contributeur
    essaies:
    xxx=range("B9").value

    +GETPIVOTDATA(""Production plant"",R44C1,""Production plant"",""1101"",""Production Date"",XXX,""Années"",2018)"
    End If
    End Sub


    Quand tu lances ta macro , tu fais ton getpivot uniquement pour la valeur de B9 ou cette valeur passe de 1 à 12 automatiquement ?

    mais fais voir ton code en entier ou, mieux, le classeur et explique ce que tu voudrais faire