Sub Save_DPV()
Dim GestionFichier As New Scripting.FileSystemObject
' On commence par définir une variable de type Folder :
Dim Dossier As Folder, res As String, IDclient As Integer, LGidclient As Integer, chemin As String
Dim Creation As String
Dim nomfichier As String
modele = ThisWorkbook.Path & "\" & ThisWorkbook.Name
ActiveSheet.Unprotect
IDclient = Sheets(1).Range("h4")
nomclient = Sheets(1).Range("D8").Value
LGidclient = Len(Sheets(1).Range("h4"))
nomfichier = "DPV " & nomclient & "_" & IDclient & " - " & Format(Date, "dd-mm-yy") & ".xlsm"
chemin = "\Dossiers clients\"
' Cette boucle parcourt tous les dossiers
For Each Dossier In GestionFichier.GetFolder(ThisWorkbook.Path & chemin).SubFolders
res = Left(Dossier.Name, LGidclient)
'MsgBox res & " " & IDclient
If res = IDclient Then
'sauvegarde du fichier Excel
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & chemin & Dossier.Name & "\" & nomfichier
Application.DisplayAlerts = False
Workbooks(nomfichier).Close SaveChanges:=True
Application.DisplayAlerts = True
Set GestionFichier = Nothing
Exit Sub
End If
Next
' si le dossier n'existe pas création
Creation = ThisWorkbook.Path & chemin & IDclient & " - " & nomclient & "\"
MkDir (Creation)
'puis sauvegarde du fichier Excel
ActiveSheet.Protect
Workbook.SaveAs Filename:=Creation & nomfichier
Application.DisplayAlerts = False
Workbooks(nomfichier).Close SaveChanges:=True
Application.DisplayAlerts = True
Set GestionFichier = Nothing
End Sub