Function MoisFormule$(c As Range, Optional incremente As Boolean, Optional formule As Boolean)
'utilisée dans la macro Situation et en B22 de la feuille de calcul
Dim txt$, i%, j%, dat$
txt = c.Formula
For i = 1 To Len(txt)
If IsNumeric(Mid(txt, i, 1)) Then Exit For '1er chiffre
Next
For j = Len(txt) To 1 Step -1
If IsNumeric(Mid(txt, j, 1)) Then Exit For 'dernier chiffre
Next
If j > i Then dat = Mid(txt, i, j - i + 1)
If Not IsDate(dat) Then Exit Function
If formule Then
MoisFormule = Left(txt, i - 1) & Format(DateSerial(Year(dat), Month(dat) + 2, 0), "dd-mm-yyyy") & Mid(txt, i + Len(dat))
Else
MoisFormule = UCase(Format(DateAdd("m", -incremente, dat), "mmmm yyyy"))
End If
End Function
Sub Situation()
Dim w As Worksheet, txt$, n%, OldName$, s1#, s2#, lig, pctmax#, colE, colF, numero$, pct#, vis
For Each w In Worksheets
If Left(w.Name, 4) = "SIT-" Then If Left(w.Name, 6) > txt Then txt = Left(w.Name, 6)
Next
If txt = "" Then Exit Sub
For Each w In Worksheets
If Left(w.Name, 7) = txt & "-" Then
If Val(Mid(w.Name, 8)) > n Then
n = Val(Mid(w.Name, 8))
OldName = txt & "-" & n
s1 = s1 + w.[E22]
s2 = s2 + w.[E34]
End If
End If
Next
If n = 0 Then OldName = txt
With Sheets("ECHEANCIER")
lig = Application.Match(Val(Mid(txt, 5, 2)), .[B:B], 0)
If IsError(lig) Then Exit Sub
pctmax = Application.Round(100 * (1 - s1 / IIf(.Cells(lig, "E") = "", 1, .Cells(lig, "E"))), 2)
If pctmax = 0 Then n = 0: pctmax = 100: s1 = 0: s2 = 0
If pctmax = 100 Then lig = lig + 1 'nouvelle tranche de travaux
colE = .Cells(lig, "E")
colF = .Cells(lig, "F")
numero = Format(.Cells(lig, "B"), "00")
End With
Do
txt = InputBox("Entrez le % des travaux réalisés ce mois :", _
"SITUATION N° " & numero & IIf(n, "-" & n + 1, "") & " # FIN " & MoisFormule(Sheets(OldName).[A12], True), pctmax)
If txt = "" Then Exit Sub
pct = Application.Round(Abs(Val(Replace(Replace(txt, ",", "."), "%", ""))), 2)
Loop While pct > pctmax
Application.ScreenUpdating = False
Application.Goto ActiveSheet.[A1], True 'cadrage
'ThisWorkbook.Unprotect "susaita" 'déprotection du classeur, mdp à adapter
With Sheets(OldName)
vis = .Visible
.Visible = True 'si la feuille est masquée
.Copy After:=Sheets(Sheets.Count)
.Visible = vis
End With
With Sheets(Sheets.Count)
'.Protect "susaita", UserInterfaceOnly:=True 'protection de la nouvelle feuille, mdp à adapter
.Name = "SIT-" & numero & IIf(n Or pct < 100, "-" & n + 1, "")
txt = MoisFormule(.[A12], True, True)
If txt = "" Then
MsgBox "Revoyez la formule en A12..."
Else
.[A12] = txt
End If
.[D22] = "='" & OldName & "'!C22"
.[E22] = IIf(pct = pctmax, colE - s1, Application.Round(colE * pct / 100, 2))
.[D34] = "='" & OldName & "'!C34"
.[E34] = IIf(pct = pctmax, colF - s2, Application.Round(colF * pct / 100, 2))
Application.Goto .[A1], True 'cadrage
End With
'ThisWorkbook.Protect "susaita" 'protection du classeur, mdp à adapter
End Sub