Plantage inexpliqué VBA Macro de création de dossier

Kran

XLDnaute Nouveau
Rebonjour le Forum,

J'ai un problème inexpliqué par VBA et inexplicable par moi:confused:. alors j'espère que vous pourrez m'aider sinon je reste dans la panade.

Voici le code
Code:
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

Qui fonctionne très bien... lorsque je le lance une 1ère puis une 2ème fois, mais 3ème fois ça plante sans message d'erreur... (ALT+CTRL+SUPPR et tout le toutim).

Help?
 

Discussions similaires

Réponses
2
Affichages
274

Statistiques des forums

Discussions
312 464
Messages
2 088 645
Membres
103 902
dernier inscrit
ZENDA