Sauvegarder un fichier en ajoutant 1 chiffre
Invité
Bonjour,
Pouvez-vous m'aider
J'aimerais sauvegarder mon fichier "toto.xlsm' quand je clic à chaque fois sur le bouton 'bidule'
avec ThisWorkbook.Save
mais qu'il m'ajoute un chiffre à la fin de toto
donc toto1.xlsm, toto2.xlsm, toto3.xlsm, etc
Est-ce possible?
merci
Invité
Bonsoir Geoffroy63
Bien sur que c'est possible, voici une petite sub générique (il peut y avoir mieux)
Sub SauvegardeAvecIncrément(sPath As String, sNomFic As String)
Dim Inc As Integer, NbNum As Integer
Dim sFic As String, sFicTmp As String, sExt As String
' Vérifier si existe dernier Slash, sinon l'ajouter
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
' 2 chiffres pour l'incrémentation - Max 5
NbNum = 2
' Vérifier si fichier existe
sFic = Dir(sPath & sNomFic)
Do While sFic <> ""
sExt = Mid(sFic, InStrRev(sFic, "."))
sFicTmp = Left(sFic, Len(sFic) - Len(sExt))
If IsNumeric(Right(sFicTmp, NbNum)) Then
Inc = Val(Right(sFicTmp, NbNum))
Inc = Inc + 1
sFicTmp = Left(sFicTmp, Len(sFicTmp) - NbNum) & Format(Inc, Left("00000", NbNum)) & sExt
Else
Inc = 1
sFicTmp = sFicTmp & Format(Inc, Left("00000", NbNum)) & sExt
End If
sFic = Dir(sPath & sFicTmp)
Loop
ThisWorkbook.SaveAs sPath & sFicTmp
End SubEt on peut l'appeller par
Sub Test()
Call SauvegardeAvecIncrément(ThisWorkbook.Path, ThisWorkbook.Name)
End SubA+
Invité
Bonjour et merci beaucoup,
Je vais de suite essayer ça, je cours
Là, suis sur mon portable.
@bientôt