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
| Function NewNameSaveAs(Chm As String, Prefix_Nom As String, Feuille As String) As String
Dim wb As Workbook, L As Range, Fichier$, NumNom$, N As Integer
ThisWorkbook.Sheets(Feuille).Copy
Set wb = ActiveWorkbook
Application.ScreenUpdating = False
With wb.ActiveSheet
.UsedRange.Value = .UsedRange.Value
.UsedRange.Sort Key1:=wb.ActiveSheet.Range("A1"), Order1:=xlAscending
Set L = .Columns(1).Find("#N/A", LookIn:=xlValues, lookat:=xlWhole)
.Rows(L.Row & ":" & .Cells(Rows.Count, 1).End(xlUp).Row).Clear
End With
Application.ScreenUpdating = True
Fichier = Dir(Chm, vbDirectory)
Do Until Fichier = vbNullString
If Fichier Like Prefix_Nom & "*.xlsx" Then
NumNom = Mid(Fichier, InStrRev(Fichier, "_") + 1, InStrRev(Fichier, ".") - InStrRev(Fichier, "_") - 1)
If IsNumeric(NumNom) And NumNom > N Then N = NumNom
End If
Fichier = Dir()
Loop
NewNameSaveAs = Prefix_Nom & Format(N + 1, "00") & ".xlsx"
Application.DisplayAlerts = False
wb.SaveAs FileName:=Chm & NewNameSaveAs, FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
wb.Close False: Set wb = Nothing: Set L = Nothing
End Function |
Partager