Sub CS_RenamerML()
'Application.Visible = False
monrep = "S:\\CS - Copie" & "\"
Set awb = Workbooks(ActiveWorkbook.Name).Sheets(ActiveSheet.Name)
r = Cells(65536, 6).End(xlUp).Row
For a = 3 To r
'---------------------------------------------CS renamer-------------------------------------------------------
If Dir(monrep & "*" & awb.Cells(a, 6) & "*") <> "" Then
monrepstyl = monrep & awb.Cells(a, 6) & "\"
monfic = Dir(monrep & "*" & awb.Cells(a, 6) & "*")
Do While monfic <> ""
If Len(Dir(monrepstyl, vbDirectory)) = 0 Then
MkDir monrepstyl 'if not you create it
End If
If Len(Dir(monrep & awb.Cells(a, 6) & "\" & monfic, vbDirectory)) = 0 Then
Name monrep & monfic As monrep & awb.Cells(a, 6) & "\" & monfic
End If
monfic = Dir(monrep & "*" & awb.Cells(a, 6) & "*")
Loop
monfic = Dir(monrepstyl & "*")
derdate = DateSerial(1, 1, 1)
Do While monfic <> ""
If FileDateTime(monrepstyl & monfic) > derdate Then
ledernier = monrepstyl & monfic
derdate = FileDateTime(monrepstyl & monfic)
End If
monfic = Dir
Loop
If Right(ledernier, 5) = ".xlsx" Then
Name ledernier As monrep & awb.Cells(a, 6) & ".xlsx"
ElseIf Right(ledernier, 3) = ".xls" Then
Name ledernier As monrep & awb.Cells(a, 6) & ".xls"
End If
End If
Next
End Sub