Microsoft 365 VBA : Calculer la date de fin de trimestre

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 !

VBA_dev_Anne_Marie

XLDnaute Occasionnel
Bonjour,

Je n'arrive pas à dérouler la date de fin trimestre sur 10 ans par rapport à la date paramétrée.
En fait, je ne trouve pas de fonction VBA qui calcule cette date. Voici mon code :
VB:
Sub Trimestre()


Dim iMonth As Integer
    iMonth = Int((Month(Sheets("Paramétrage").Cells(3, 3).Value) - 1) / 3) * 3 + 4


Sheets("Paramétrage").Cells(12, 2).Value = EOQuarter = DateSerial(Year(Sheets("Paramétrage").Cells(3, 3).Value), iMonth, 0)
 
If Month(Sheets("Paramétrage").Cells(3, 3).Value) >= 1 And Month(Sheets("Paramétrage").Cells(3, 3).Value) <= 3 Then
Sheets("Paramétrage").Cells(12, 1).Value = "T1"
End If


If Month(Sheets("Paramétrage").Cells(3, 3).Value) > 3 And Month(Sheets("Paramétrage").Cells(3, 3).Value) <= 6 Then
Sheets("Paramétrage").Cells(12, 1).Value = "T2"
End If
 
 
If Month(Sheets("Paramétrage").Cells(3, 3).Value) > 6 And Month(Sheets("Paramétrage").Cells(3, 3).Value) <= 9 Then
Sheets("Paramétrage").Cells(12, 1).Value = "T3"

End If
 
 
If Month(Sheets("Paramétrage").Cells(3, 3).Value) > 9 And Month(Sheets("Paramétrage").Cells(3, 3).Value) <= 12 Then
Sheets("Paramétrage").Cells(12, 1).Value = "T4"
End If
 
 
 
End Sub

J'ai joint le fichier xlsm avec les résultats attendus.
 

Pièces jointes

Solution
Bonsoir @VBA_dev_Anne_Marie,

Pour ce que j'en ai compris, essayez le code qui suit.
La constante NbreTrimestres représente le nombre de trimestres à afficher (ici 40 car 10 ans c'est 40 trimestres)
Le code :
VB:
Sub Ntrimestres()
Const NbreTrimestres = 40
Dim d1 As Date, i As Long
   With Sheets("Paramétrage")
      .Range(.Range("a12"), .Range("a12").End(xlDown)).Resize(, 2).Clear
      d1 = .Range("c3")
      ReDim res(1 To NbreTrimestres, 1 To 2)
      For i = 1 To NbreTrimestres
         res(i, 2) = DateSerial(Year(d1), 1 + 3 * Format(d1, "q") + (i - 1) * 3, 1) - 1
         res(i, 1) = "T" & Format(res(i, 2), "q")
      Next i
      .Range("a12").Resize(NbreTrimestres, 2) = res
   End With
End Sub
Sub Trimestre()
Dim iMonth As Integer
Dim iYear As Integer
iMonth = Int((Month(Sheets("Paramétrage").Cells(3, 3).Value) - 1) / 3) * 3 + 4
iYear = Year(Sheets("Paramétrage").Cells(3, 3).Value)
For i = 1 To 10
Sheets("Paramétrage").Cells(12 + i - 1, 2).Value = EOQuarter(iMonth, iYear)
If iMonth = 4 Then
iMonth = 1
iYear = iYear + 1
Else
iMonth = iMonth + 3
End If
Next i
End Sub
 
- 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

  • Question Question
XL 2021 VBA excel
Réponses
4
Affichages
74
Réponses
3
Affichages
235
  • Question Question
Microsoft 365 worksheet_change
Réponses
29
Affichages
246
Réponses
4
Affichages
359
Retour