1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| Option Explicit
Dim Fs, Chemin$, Fich$, num!, numt$, numpre!, lg As Byte
Sub Enregister()
Set Fs = Application.FileSearch
Chemin = "C:\Factures\"
With Fs
.LookIn = Chemin
.Filename = "Facture *.xls"
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) = 0 Then
MsgBox "Aucun fichier correspondant trouvé dans le répertoire indiqué."
Exit Sub
End If
End With
Application.DisplayAlerts = False
Fich = Dir(Chemin & "Facture *.xls")
numpre = 0
Do While Fich <> ""
numpre = numpre + 1
num = Mid(Fich, 9, 4)
If num > numpre Then
lg = Len(CStr(num))
numt = Left("000", 4 - lg) & CStr(numpre)
ActiveWorkbook.SaveAs ActiveWorkbook.Path & "\facture " & numt
Range("B9,A14:A24,E14:E24").ClearContents
ActiveWorkbook.SaveAs ActiveWorkbook.Path & "\facture_modele"
Exit Sub
End If
Fich = Dir
Loop
num = Range("b10")
lg = Len(CStr(num))
numt = Left("000", 4 - lg) & CStr(num)
ActiveWorkbook.SaveAs ActiveWorkbook.Path & "\facture " & numt
Range("B9,A14:A24,E14:E24").ClearContents
Range("B10") = Range("B10") + 1
ActiveWorkbook.SaveAs ActiveWorkbook.Path & "\facture_modele"
Application.DisplayAlerts = True
End Sub |
Partager