Sub Facture()
Dim w As Worksheet, txt$, n%, OldName$, s1#, s2#, lig, pctmax#, colC, colE, colF, numero$, pct#, vis
For Each w In Worksheets
If Left(w.Name, 4) = "FAC-" 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.[C23]
s2 = s2 + w.[C24]
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
colC = .Cells(lig, "C")
colE = .Cells(lig, "E")
colF = .Cells(lig, "F")
numero = Format(.Cells(lig, "B"), "00")
If Not numero Like "##" Then Exit Sub 'si les derniers travaux ont été réalisés
End With
Do
txt = InputBox("Entrez le % des travaux réalisés :", "FAC-" & numero & IIf(n, "-" & n + 1, ""), 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 "BoixosNois" '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 "BoixosNois", UserInterfaceOnly:=True 'protection de la nouvelle feuille, mdp à adapter
.Name = "FAC-" & numero & IIf(n Or pct < 100, "-" & n + 1, "")
.[B23] = UCase(colC)
.[C23] = IIf(pct = pctmax, colE - s1, Application.Round(colE * pct / 100, 2))
.[C24] = IIf(pct = pctmax, colF - s2, Application.Round(colF * pct / 100, 2))
Application.Goto .[A1], True 'cadrage
End With
'ThisWorkbook.Protect "BoixosNois" 'protection du classeur, mdp à adapter
End Sub