est toujours d'actualité, car si tu te perds dans tes posts .... que devrions nous direComment faire choisir à l'utilisateur les feuilles où il veut que la fonction s'exécute.
Private Sub ComboBox1_Change()
Dim TabSht() As String ' Définir le tableau des feuilles
TabSht = Split("Janvier,Mars,Juin", ",")
Label1.Caption = Periode(TabSht, ComboBox1.ListIndex + 4) / 2 & " jours consécutifs "
End Sub
' Fonction qui selon le tableau des feuilles : TabSht()
' Comptabilise les cellules
Function Periode(TabSht() As String, Optional Lig As Long = 2)
Application.Volatile 'selon le besoin…
Dim Ind As Integer, Rng As Range
Dim Cpte As Integer
For Ind = 0 To UBound(TabSht)
With Sheets(TabSht(Ind))
If Lig = 0 Then Lig = 4
Set Rng = .Range("C" & Lig)
Do While IsDate(.Cells(1, Rng.Column))
If IsEmpty(Rng) = True Then
Cpte = Cpte + 1
Else
Cpte = 0
End If
If Periode < Cpte Then Periode = Cpte
Set Rng = Rng(1, 2)
Loop
End With
Next
End Function
Private Sub ComboBox1_Change()
Label1.Caption = Periode(Me.ComboBox1.Value, ComboBox1.ListIndex + 4) / 2 & " jours consécutifs "
End Sub
' Fonction qui selon la feuille : Sht
' Comptabilise les cellules
Function Periode(Sht As String, Optional Lig As Long = 2)
Application.Volatile 'selon le besoin…
Dim Ind As Integer, Rng As Range
Dim Cpte As Integer
With Sheets(Sht)
If Lig = 0 Then Lig = 4
Set Rng = .Range("C" & Lig)
Do While IsDate(.Cells(1, Rng.Column))
If IsEmpty(Rng) = True Then
Cpte = Cpte + 1
Else
Cpte = 0
End If
If Periode < Cpte Then Periode = Cpte
Set Rng = Rng(1, 2)
Loop
End With
End Function
Result = Periode(TabSht, 4 + LigSel)
Function PeriodeG(TabSht() As String, Optional Lig As Long = 2)
Dim Ind As Integer, Rng As Range
Dim Cpte As Integer, NbG As Integer, NbVide As Integer
' Initialisation des variables
Cpte = 0: NbG = 0: NbVide = 0
' Application.Volatile ' En cas de besoin
' Pour chaque Item du tableau
For Ind = 0 To UBound(TabSht)
' Avec la feuille du nom du tableau
With Sheets(TabSht(Ind))
' Définir la cellule de départ
Set Rng = .Range("C" & Lig)
' Si la colonne de la ligne 1 est une date
Do While IsDate(.Cells(1, Rng.Column))
' Si la cellule contient "G"
If Rng.Value = "G" Then
NbG = NbG + 1
ElseIf Rng.Value <> "" Then
NbG = 0: NbVide = 0
End If
' Sinon si la cellule est vide
If Rng.Value = "" Then
' Si le nombre de G est de 2
If NbG = 2 Then NbVide = NbVide + 1 Else NbG = 0
End If
' Si les 2 conditions sont remplies
If NbG = 2 And NbVide = 3 Then
' On ajoute 1 au compte
Cpte = Cpte + 1
NbG = 0: NbVide = 0
End If
Set Rng = Rng(1, 2)
Loop
End With
Next
PeriodeG = Cpte
End Function