Microsoft 365 VBA : Calculer la date de fin de trimestre

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

  • Paramétrage.xlsm
    13.2 KB · Affichages: 11
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

eCHO

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

Statistiques des forums

Discussions
312 103
Messages
2 085 310
Membres
102 859
dernier inscrit
Diallokass